Don't print stacktrace on exceptions specific to CliFx domain

This commit is contained in:
Alexey Golub
2019-08-11 21:03:08 +03:00
parent 6a06cdc422
commit f8cd2a56b2
5 changed files with 41 additions and 10 deletions

View File

@@ -48,8 +48,10 @@ 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;
}
@@ -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;
}
}
}

View File

@@ -2,7 +2,7 @@
namespace CliFx.Exceptions
{
public class CannotConvertCommandOptionException : Exception
public class CannotConvertCommandOptionException : CliFxException
{
public CannotConvertCommandOptionException()
{

View File

@@ -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()
{
}
}
}

View File

@@ -2,7 +2,7 @@
namespace CliFx.Exceptions
{
public class CommandErrorException : Exception
public class CommandErrorException : CliFxException
{
public int ExitCode { get; }

View File

@@ -2,7 +2,7 @@
namespace CliFx.Exceptions
{
public class MissingCommandOptionException : Exception
public class MissingCommandOptionException : CliFxException
{
public MissingCommandOptionException()
{