Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add catch snippet #4094

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading