diff --git a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormat.cs b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormat.cs index bc1977b..30fac6d 100644 --- a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormat.cs +++ b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormat.cs @@ -35,4 +35,9 @@ public enum ExceptionFormats /// Shortens everything that can be shortened. /// ShortenEverything = ShortenMethods | ShortenTypes | ShortenPaths, + + /// + /// Whether or not to show the exception stack trace. + /// + NoStackTrace = 16, } \ No newline at end of file diff --git a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs index 97acddf..0313910 100644 --- a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs +++ b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs @@ -50,6 +50,11 @@ internal static class ExceptionFormatter } // Stack frames + if ((settings.Format & ExceptionFormats.NoStackTrace) != 0) + { + return grid; + } + var stackTrace = new StackTrace(ex, fNeedFileInfo: true); var frames = stackTrace .GetFrames() diff --git a/test/Spectre.Console.Tests/Expectations/Exception/NoStackTrace.Output.verified.txt b/test/Spectre.Console.Tests/Expectations/Exception/NoStackTrace.Output.verified.txt new file mode 100644 index 0000000..7f20276 --- /dev/null +++ b/test/Spectre.Console.Tests/Expectations/Exception/NoStackTrace.Output.verified.txt @@ -0,0 +1,2 @@ +System.InvalidOperationException: Something threw! + System.InvalidOperationException: Throwing! diff --git a/test/Spectre.Console.Tests/Unit/ExceptionTests.cs b/test/Spectre.Console.Tests/Unit/ExceptionTests.cs index a1c40df..a8eb258 100644 --- a/test/Spectre.Console.Tests/Unit/ExceptionTests.cs +++ b/test/Spectre.Console.Tests/Unit/ExceptionTests.cs @@ -108,6 +108,21 @@ public sealed class ExceptionTests 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