Skip to content

Commit e16dfce

Browse files
authored
wrap logic in try-catch and log errors (#106)
* wrap logic in try-catch and log errors * rename and use Safely as method suffix
1 parent 0814c71 commit e16dfce

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsAnalyzer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
7+
using System.Diagnostics;
78
using System.Linq;
89

910
namespace FluentAssertions.Analyzers
@@ -33,7 +34,7 @@ private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
3334
{
3435
foreach (var statement in method.Body.Statements.OfType<ExpressionStatementSyntax>())
3536
{
36-
var diagnostic = AnalyzeExpression(statement.Expression, context.SemanticModel);
37+
var diagnostic = AnalyzeExpressionSafely(statement.Expression, context.SemanticModel);
3738
if (diagnostic != null)
3839
{
3940
context.ReportDiagnostic(diagnostic);
@@ -43,7 +44,7 @@ private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
4344
}
4445
if (method.ExpressionBody != null)
4546
{
46-
var diagnostic = AnalyzeExpression(method.ExpressionBody.Expression, context.SemanticModel);
47+
var diagnostic = AnalyzeExpressionSafely(method.ExpressionBody.Expression, context.SemanticModel);
4748
if (diagnostic != null)
4849
{
4950
context.ReportDiagnostic(diagnostic);
@@ -86,6 +87,19 @@ protected virtual Diagnostic CreateDiagnostic(TCSharpSyntaxVisitor visitor, Expr
8687
location: expression.GetLocation(),
8788
properties: properties);
8889
}
90+
91+
private Diagnostic AnalyzeExpressionSafely(ExpressionSyntax expression, SemanticModel semanticModel)
92+
{
93+
try
94+
{
95+
return AnalyzeExpression(expression, semanticModel);
96+
}
97+
catch (Exception e)
98+
{
99+
Console.Error.WriteLine($"Failed to analyze expression in {GetType().FullName}.\n{e}");
100+
return null;
101+
}
102+
}
89103
}
90104

91105
public abstract class FluentAssertionsAnalyzer : FluentAssertionsAnalyzer<FluentAssertionsCSharpSyntaxVisitor>

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsCodeFixProvider.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.CodeFixes;
44
using Microsoft.CodeAnalysis.CSharp;
55
using Microsoft.CodeAnalysis.CSharp.Syntax;
6+
using System;
67
using System.Collections.Generic;
78
using System.Collections.Immutable;
89
using System.Linq;
@@ -38,7 +39,7 @@ protected async Task<Document> RewriteAssertion(Document document, ExpressionSyn
3839
{
3940
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
4041

41-
var newExpression = GetNewExpression(expression, new FluentAssertionsDiagnosticProperties(properties));
42+
var newExpression = GetNewExpressionSafely(expression, new FluentAssertionsDiagnosticProperties(properties));
4243

4344
root = root.ReplaceNode(expression, newExpression);
4445

@@ -89,5 +90,18 @@ protected ExpressionSyntax RenameIdentifier(ExpressionSyntax expression, string
8990
.First(node => node.Identifier.Text == oldName);
9091
return expression.ReplaceNode(identifierNode, identifierNode.WithIdentifier(SyntaxFactory.Identifier(newName).WithTriviaFrom(identifierNode.Identifier)));
9192
}
93+
94+
private ExpressionSyntax GetNewExpressionSafely(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
95+
{
96+
try
97+
{
98+
return GetNewExpression(expression, properties);
99+
}
100+
catch (Exception e)
101+
{
102+
Console.Error.WriteLine($"Failed to get new expression in {GetType().FullName}.\n{e}");
103+
return expression;
104+
}
105+
}
92106
}
93107
}

0 commit comments

Comments
 (0)