Skip to content

Commit

Permalink
Do not print the stack trace for ArgumentException #112
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jun 6, 2024
1 parent 97a1881 commit b188553
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static partial class ConsoleApp
catch (Exception ex)
{
Environment.ExitCode = 1;
if (ex is ValidationException)
if (ex is ValidationException or ArgumentParseFailedException)
{
LogError(ex.Message);
}
Expand Down
5 changes: 3 additions & 2 deletions sandbox/GeneratorSandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@



args = ["show", "--aaa", "a", "--value", "10.2"];
args = ["show", "--aaa", "a", "value", "10.2"];

var app = ConsoleApp.Create();
app.Add<Test>();
Expand All @@ -13,4 +13,5 @@
public class Test
{
public void Show(string aaa, [Range(0, 1)] double value) => ConsoleApp.Log($"{value}");
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public abstract class ConsoleAppFilter(ConsoleAppFilter next)
public sealed class ConsoleAppFilterAttribute<T> : Attribute
where T : ConsoleAppFilter
{
}

public sealed class ArgumentParseFailedException(string message) : Exception(message)
{
}
12 changes: 8 additions & 4 deletions src/ConsoleAppFramework/ConsoleAppGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ internal sealed class ConsoleAppFilterAttribute<T> : Attribute
{
}

internal sealed class ArgumentParseFailedException(string message) : Exception(message)
{
}

#endif

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
Expand Down Expand Up @@ -179,17 +183,17 @@ public static Task RunAsync(string[] args)

static void ThrowArgumentParseFailed(string argumentName, string value)
{
throw new ArgumentException($"Argument '{argumentName}' failed to parse, provided value: {value}");
throw new ArgumentParseFailedException($"Argument '{argumentName}' failed to parse, provided value: {value}");
}

static void ThrowRequiredArgumentNotParsed(string name)
{
throw new ArgumentException($"Required argument '{name}' was not specified.");
throw new ArgumentParseFailedException($"Required argument '{name}' was not specified.");
}

static void ThrowArgumentNameNotFound(string argumentName)
{
throw new ArgumentException($"Argument '{argumentName}' is not recognized.");
throw new ArgumentParseFailedException($"Argument '{argumentName}' is not recognized.");
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -364,7 +368,7 @@ static async Task RunWithFilterAsync(string commandName, string[] args, ConsoleA
}

Environment.ExitCode = 1;
if (ex is ValidationException)
if (ex is ValidationException or ArgumentParseFailedException)
{
LogError(ex.Message);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleAppFramework/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy

sb.AppendLine("Environment.ExitCode = 1;");

using (sb.BeginBlock("if (ex is ValidationException)"))
using (sb.BeginBlock("if (ex is ValidationException or ArgumentParseFailedException)"))
{
sb.AppendLine("LogError(ex.Message);");
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ConsoleAppFramework.GeneratorTests/RunTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public void SyncRunShouldFailed()
verifier.Error("ConsoleApp.Run(args, (int x) => { Console.Write((x)); });", "--x").Should().Contain("Argument 'x' failed to parse");
}

[Fact]
public void MissingArgument()
{
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").Should().Contain("Argument 'y' is not recognized.");

Environment.ExitCode.Should().Be(1);
Environment.ExitCode = 0;
}

[Fact]
public void ValidateOne()
{
Expand Down

0 comments on commit b188553

Please sign in to comment.