Skip to content

Commit

Permalink
Add catch snippet (#4094)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft committed Aug 5, 2024
1 parent 72d4351 commit e8e816e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,12 @@ public static MethodBodyStatement Declare<T>(string name, ScopedApi<T> value, ou
variable = variableExpression.As<T>();
return new DeclarationExpression(variableExpression).Assign(value).Terminate();
}

public static DeclarationExpression Declare<T>(string name, out ScopedApi<T> variable)
{
var variableExpression = new VariableExpression(typeof(T), name);
variable = variableExpression.As<T>();
return new DeclarationExpression(variableExpression);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public static ValueExpression ArrayEmpty(CSharpType arrayItemType)
public static AssignmentExpression Assign(this FieldProvider to, ValueExpression value, bool nullCoalesce = false) => new AssignmentExpression(to, value, nullCoalesce);
public static AssignmentExpression Assign(this PropertyProvider to, ValueExpression value, bool nullCoalesce = false) => new AssignmentExpression(to, value, nullCoalesce);

public static CatchStatement Catch(DeclarationExpression declare, params MethodBodyStatement[] statements) => new CatchStatement(declare) { statements };

public static MethodBodyStatement InvokeConsoleWriteLine(ValueExpression expression)
=> Static(typeof(Console)).Invoke(nameof(Console.WriteLine), expression).Terminate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Snippets;
using Microsoft.Generator.CSharp.Statements;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

Expand Down Expand Up @@ -36,19 +37,17 @@ protected override IReadOnlyList<MethodProvider> BuildMethods()

// Convert to a method with a body statement so we can add tracing.
ConvertToBodyStatementMethodProvider(method);
var ex = new VariableExpression(typeof(Exception), "ex");
var decl = new DeclarationExpression(ex);
var statements = new TryCatchFinallyStatement(
[
InvokeConsoleWriteLine(Literal($"Entering method {method.Signature.Name}.")),
method.BodyStatements!
],
new CatchStatement(decl)
{
InvokeConsoleWriteLine(new FormattableStringExpression(
$"An exception was thrown in method {method.Signature.Name}: {{0}}", new[] { ex })),
Throw()
},
Catch(Declare("ex", out ScopedApi<Exception> ex),
[
InvokeConsoleWriteLine(new FormattableStringExpression(
$"An exception was thrown in method {method.Signature.Name}: {{0}}", new[] { ex })),
Throw()
]),
[InvokeConsoleWriteLine(Literal($"Exiting method {method.Signature.Name}."))]);

method.Update(bodyStatements: statements);
Expand Down

0 comments on commit e8e816e

Please sign in to comment.