Skip to content

IndexOutOfRangeException thrown by AnsiConsoleExtensions.WriteException if no stack trace #1798

@martincostello

Description

@martincostello

Information

  • OS: Any
  • Version: 0.50.0
  • Terminal: N/A

Describe the bug

If an exception with no stack trace is passed to AnsiConsoleExtensions.WriteException(), then an IndexOutOfRangeException is thrown:

System.IndexOutOfRangeException
  HResult=0x80131508
  Message=Index was outside the bounds of the array.
  Source=System.Private.CoreLib
  StackTrace:
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException()
   at Spectre.Console.ExceptionFormatter.GetStackFrames(Exception ex, ExceptionSettings settings)
   at Spectre.Console.ExceptionFormatter.GetException(Exception exception, ExceptionSettings settings)
   at Spectre.Console.ExceptionFormatter.Format(Exception exception, ExceptionSettings settings)
   at Spectre.Console.ExceptionExtensions.GetRenderable(Exception exception, ExceptionSettings settings)
   at Spectre.Console.ExceptionExtensions.GetRenderable(Exception exception, ExceptionFormats format)
   at Spectre.Console.AnsiConsoleExtensions.WriteException(IAnsiConsole console, Exception exception, ExceptionFormats format)
   at MartinCostello.DotNetBumper.IAnsiConsoleExtensions.WriteExceptionLine(IAnsiConsole console, String message, Exception exception) in D:\Coding\martincostello\dotnet-bumper\src\DotNetBumper.Core\IAnsiConsoleExtensions.cs:line 115
   at MartinCostello.DotNetBumper.IAnsiConsoleExtensionsTests.<>c__DisplayClass4_0.<WriteExceptionLine_Does_Not_Throw>b__0() in D:\Coding\martincostello\dotnet-bumper\tests\DotNetBumper.Tests\IAnsiConsoleExtensionsTests.cs:line 76
   at Shouldly.Should.NotThrowInternal(Action action, String customMessage, String shouldlyMethod)

I found this as I'd written a test to test an extension method I'd written (below) and I just create an exception and pass it in to verify no exception is thrown, and with dependabot's PR to update my code to 0.50.0, that test now fails:

public static void WriteExceptionLine(this IAnsiConsole console, string message, Exception exception)
{
    console.WriteErrorLine(message);
    console.WriteException(exception);
}

[Fact]
public static void WriteExceptionLine_Does_Not_Throw()
{
    // Arrange
    var console = Substitute.For<IAnsiConsole>();

    // Act and Assert
    Should.NotThrow(() => console.WriteExceptionLine("An error.", new InvalidOperationException("An exception.")));
}

To Reproduce

[Fact]
public static void Repro()
{
    var console = Substitute.For<IAnsiConsole>();

    Should.NotThrow(() => console.WriteException(new InvalidOperationException("An exception.")));
}

Expected behavior

No exception is thrown.


Please upvote 👍 this issue if you are interested in it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions