mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-16 04:03:22 +08:00
Fix IndexOutOfRangeException
Fix `IndexOutOfRangeException` if an exception does not have an associated stack trace. Resolves #1798.
This commit is contained in:
parent
b5c839030c
commit
6105ee2a86
@ -65,7 +65,7 @@ internal static class ExceptionFormatter
|
||||
|
||||
var stackTrace = new StackTrace(ex, fNeedFileInfo: true);
|
||||
var allFrames = stackTrace.GetFrames();
|
||||
if (allFrames[0]?.GetMethod() == null)
|
||||
if (allFrames.Length > 0 && allFrames[0]?.GetMethod() == null)
|
||||
{
|
||||
// if we can't easily get the method for the frame, then we are in AOT
|
||||
// fallback to using ToString method of each frame.
|
||||
|
@ -142,4 +142,21 @@ public partial class AnsiConsoleTests
|
||||
.ShouldBe("[101mHello[0m\n[101mWorld[0m\n");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class WriteException
|
||||
{
|
||||
[Fact]
|
||||
public void Should_Not_Throw_If_Exception_Has_No_StackTrace()
|
||||
{
|
||||
// Given
|
||||
var console = new TestConsole();
|
||||
var exception = new InvalidOperationException("An exception.");
|
||||
|
||||
// When
|
||||
void When() => console.WriteException(exception);
|
||||
|
||||
// Then
|
||||
Should.NotThrow(When);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user