From 5104a2ebf91890413f7aa5f1e6a37f0550332ff7 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Fri, 16 Aug 2019 17:35:44 +0300 Subject: [PATCH] Only print error message if it's set, otherwise fallback to stack trace --- CliFx/CliApplication.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index a81b6c6..a8cc002 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -186,7 +186,15 @@ namespace CliFx } catch (Exception ex) { - var message = ex is CliFxException ? ex.Message : ex.ToString(); + // We want to catch exceptions in order to print error and return correct exit code. + // Also, by doing this we get rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions. + + // In case we catch a CliFx-specific exception, we want to just show the error message, not the stack trace. + // Stack trace isn't very useful to the end user if the exception is not really coming from their code. + + // CommandException is the same, but it also lets users specify exit code so we want to return that instead of default. + + var message = ex is CliFxException && !ex.Message.IsNullOrWhiteSpace() ? ex.Message : ex.ToString(); var exitCode = ex is CommandErrorException errorException ? errorException.ExitCode : -1; _console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(message));