Skip to content
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
9 changes: 9 additions & 0 deletions src/Spectre.Console.Tests/Data/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public static (string Key, List<T> Values) GetTuplesWithInnerException<T>((int F
MethodThatThrows(0);
return ("key", []);
}

public static async Task MethodThatThrowsAsync()
{
await Task.Yield();
throw new InvalidOperationException("Throwing async!");
}

public static void ThrowFromAsync() =>
MethodThatThrowsAsync().GetAwaiter().GetResult();
}

#pragma warning disable CS9113 // Parameter is unread.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
InvalidOperationException: Throwing async!
at async Task MethodThatThrowsAsync() in Exceptions.cs:100
at void ThrowFromAsync() in Exceptions.cs:200
at Exception GetException(Action action) in ExceptionTests.cs:300
19 changes: 19 additions & 0 deletions src/Spectre.Console.Tests/Unit/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ public Task Exception_Within_Expanded_Panel_Should_Expand_As_Expected()
return Verifier.Verify(console.Output);
}

[Fact]
[Expectation("AsyncStackTrace")]
public Task Should_Write_Async_Exception_With_Resolved_Method_Names()
{
// Given
var console = new TestConsole().Width(1024);
var dex = GetException(TestExceptions.ThrowFromAsync);

// When
console.WriteException(dex, new ExceptionSettings
{
Format = ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks,
Resolver = new ExceptionScrubber(),
});

// Then
return Verifier.Verify(console.Output);
}

private static Exception GetException(Action action)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private static string GetMethodName(ExceptionInfoResolver resolver, ref MethodBa
isAsync = typeof(IAsyncStateMachine).IsAssignableFrom(declaringType);
if (isAsync || typeof(IEnumerator).IsAssignableFrom(declaringType))
{
TryResolveStateMachineMethod(method, out declaringType);
TryResolveStateMachineMethod(ref method, out declaringType);
}
}
else
Expand All @@ -304,7 +304,7 @@ private static string GetMethodName(ExceptionInfoResolver resolver, ref MethodBa
}

[RequiresDynamicCode(ExceptionRenderableBuilder.AotWarning)]
private static bool TryResolveStateMachineMethod(MethodBase method, out Type declaringType)
private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
{
// https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs#L400-L455
declaringType = method.DeclaringType ??
Expand Down