Skip to content

Commit

Permalink
Merge pull request mayuki#20 from mayuki/hotfix/FixStackTrace
Browse files Browse the repository at this point in the history
Capture stack trace of exception thrown during command execution.
  • Loading branch information
mayuki authored May 28, 2020
2 parents 6f4beea + e572f01 commit d320fcb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Cocona.Command.Binder;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Cocona.Application;

Expand Down Expand Up @@ -50,7 +52,8 @@ public override async ValueTask<int> DispatchAsync(CommandDispatchContext ctx)
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
return 1; // NOTE: This statement is unreachable.
}
finally
{
Expand Down
22 changes: 22 additions & 0 deletions test/Cocona.Test/Integration/EndToEndTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,27 @@ public void Forward() { }
[CommandMethodForwardedTo(typeof(BuiltInOptionLikeCommands), nameof(BuiltInOptionLikeCommands.ShowHelp))]
public void MyHelp() { }
}

[Fact]
public void CoconaApp_Run_Throw()
{
var (stdOut, stdErr, exitCode) = Run<TestCommand_Throw>(new string[] { "my-help" });

stdErr.Should().Contain("Unhandled Exception:");
stdErr.Should().Contain("ThrowCore()");
}

class TestCommand_Throw
{
public void Throw()
{
ThrowCore();
}

private void ThrowCore()
{
throw new Exception("Exception!");
}
}
}
}

0 comments on commit d320fcb

Please sign in to comment.