Skip to content

Commit

Permalink
Added NoStackTrace to ExceptionFormats (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardog authored Mar 8, 2024
1 parent d921ac6 commit e66d3aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Spectre.Console/Widgets/Exceptions/ExceptionFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public enum ExceptionFormats
/// Shortens everything that can be shortened.
/// </summary>
ShortenEverything = ShortenMethods | ShortenTypes | ShortenPaths,

/// <summary>
/// Whether or not to show the exception stack trace.
/// </summary>
NoStackTrace = 16,
}
5 changes: 5 additions & 0 deletions src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ private static Grid GetStackFrames(Exception ex, ExceptionSettings settings)
}

// Stack frames
if ((settings.Format & ExceptionFormats.NoStackTrace) != 0)
{
return grid;
}

var stackTrace = new StackTrace(ex, fNeedFileInfo: true);
var frames = stackTrace
.GetFrames()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
System.InvalidOperationException: Something threw!
System.InvalidOperationException: Throwing!
15 changes: 15 additions & 0 deletions test/Spectre.Console.Tests/Unit/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ public Task Should_Write_Exception_With_Tuple_Return()
return Verifier.Verify(result);
}

[Fact]
[Expectation("NoStackTrace")]
public Task Should_Write_Exception_With_No_StackTrace()
{
// Given
var console = new TestConsole().Width(1024);
var dex = GetException(() => TestExceptions.ThrowWithInnerException());

// When
var result = console.WriteNormalizedException(dex, ExceptionFormats.NoStackTrace);

// Then
return Verifier.Verify(result);
}

public static Exception GetException(Action action)
{
try
Expand Down

0 comments on commit e66d3aa

Please sign in to comment.