From f8cd2a56b20dac443df3f8cd0b6171087de1da3d Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sun, 11 Aug 2019 21:03:08 +0300 Subject: [PATCH] Don't print stacktrace on exceptions specific to CliFx domain --- CliFx/CliApplication.cs | 24 +++++++++++++------ .../CannotConvertCommandOptionException.cs | 2 +- CliFx/Exceptions/CliFxException.cs | 21 ++++++++++++++++ CliFx/Exceptions/CommandErrorException.cs | 2 +- .../MissingCommandOptionException.cs | 2 +- 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 CliFx/Exceptions/CliFxException.cs diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index c6adebd..e5cfdf5 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -48,12 +48,14 @@ namespace CliFx // Fail if there are no commands defined if (!availableCommandSchemas.Any()) { - _console.WithColor(ConsoleColor.Red, - c => c.Error.WriteLine("There are no commands defined in this application.")); + _console.WithColor(ConsoleColor.Red, c => + { + c.Error.WriteLine("There are no commands defined in this application."); + }); return -1; } - + // Handle cases where requested command is not defined if (matchingCommandSchema == null) { @@ -64,8 +66,10 @@ namespace CliFx { isError = true; - _console.WithColor(ConsoleColor.Red, - c => c.Error.WriteLine($"Specified command [{commandInput.CommandName}] is not defined.")); + _console.WithColor(ConsoleColor.Red, c => + { + c.Error.WriteLine($"Specified command [{commandInput.CommandName}] is not defined."); + }); } // Get parent command schema @@ -112,9 +116,15 @@ namespace CliFx } catch (Exception ex) { - _console.WithColor(ConsoleColor.Red, c => c.Error.WriteLine(ex)); + var message = ex is CliFxException ? ex.Message : ex.ToString(); + var exitCode = ex is CommandErrorException errorException ? errorException.ExitCode : -1; - return ex is CommandErrorException errorException ? errorException.ExitCode : -1; + _console.WithColor(ConsoleColor.Red, c => + { + c.Error.WriteLine(message); + }); + + return exitCode; } } } diff --git a/CliFx/Exceptions/CannotConvertCommandOptionException.cs b/CliFx/Exceptions/CannotConvertCommandOptionException.cs index 3b76cd7..f5d1c1b 100644 --- a/CliFx/Exceptions/CannotConvertCommandOptionException.cs +++ b/CliFx/Exceptions/CannotConvertCommandOptionException.cs @@ -2,7 +2,7 @@ namespace CliFx.Exceptions { - public class CannotConvertCommandOptionException : Exception + public class CannotConvertCommandOptionException : CliFxException { public CannotConvertCommandOptionException() { diff --git a/CliFx/Exceptions/CliFxException.cs b/CliFx/Exceptions/CliFxException.cs new file mode 100644 index 0000000..058e257 --- /dev/null +++ b/CliFx/Exceptions/CliFxException.cs @@ -0,0 +1,21 @@ +using System; + +namespace CliFx.Exceptions +{ + public abstract class CliFxException : Exception + { + protected CliFxException(string message) + : base(message) + { + } + + protected CliFxException(string message, Exception innerException) + : base(message, innerException) + { + } + + protected CliFxException() + { + } + } +} \ No newline at end of file diff --git a/CliFx/Exceptions/CommandErrorException.cs b/CliFx/Exceptions/CommandErrorException.cs index 9fb89a6..28fdd9c 100644 --- a/CliFx/Exceptions/CommandErrorException.cs +++ b/CliFx/Exceptions/CommandErrorException.cs @@ -2,7 +2,7 @@ namespace CliFx.Exceptions { - public class CommandErrorException : Exception + public class CommandErrorException : CliFxException { public int ExitCode { get; } diff --git a/CliFx/Exceptions/MissingCommandOptionException.cs b/CliFx/Exceptions/MissingCommandOptionException.cs index e5c5a50..679e13e 100644 --- a/CliFx/Exceptions/MissingCommandOptionException.cs +++ b/CliFx/Exceptions/MissingCommandOptionException.cs @@ -2,7 +2,7 @@ namespace CliFx.Exceptions { - public class MissingCommandOptionException : Exception + public class MissingCommandOptionException : CliFxException { public MissingCommandOptionException() {