Getting StackTrace to print on an unhandled exception #3296
Unanswered
xforever1313
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey everyone!
I've recently been playing around with Cake.Frosting (version 1.1.0), and have been loving it. But I have a use case that I have a "workaround" for, and I was wondering if there was a less hacky way to do it.
The thing I'm looking for is, on an Unhanded Exception in Run(), I would like the entire stack trace to print out, even if the verbosity is not originally set to diagnostic. At the moment, the stack trace only seems to print out on an error if the verbosity is set to Diagnostic. It makes sense why it does this. While we could just hard-code the verbosity at the start when creating the CakeHost object, it could mean a lot of unneeded output 99% of the time, which isn't ideal for the environment I work in.
This is a sample class I have:
I tried this at first on OnError, but the issue with this method is CakeHost.Run() returns with a zero exit code:
and this is what's printed out:
> .\FrostingTest.exe --target=some_task ======================================== some_task ======================================== An error occurred when executing task 'some_task'. System.Exception: Oops at FrostingTest.SomeTask.Func2() in <redacted>:line 41 at FrostingTest.SomeTask.Func1() in <redacted>:line 36 at FrostingTest.SomeTask.Run(ICakeContext context) in <redacted>:line 31 at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.RunAsync(ICakeContext context) at Cake.Core.CakeTask.Execute(ICakeContext context) at Cake.Core.DefaultExecutionStrategy.ExecuteAsync(CakeTask task, ICakeContext context) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) Task Duration -------------------------------------------------- some_task 00:00:00.0471202 -------------------------------------------------- Total: 00:00:00.0471202 > echo $LASTEXITCODE 0
Next thing I tried was just re-throwing the exception after enabling DiagnosticVerbosity. This was closer to what I wanted, other than the stack trace starting on OnError(), not the origin point.
> .\FrostingTest.exe --target=some_task ======================================== some_task ======================================== An error occurred when executing task 'some_task'. Error: System.Exception: Oops at FrostingTest.SomeTask.OnError(Exception exception, ICakeContext context) in <redacted>:line 53 at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.OnError(Exception exception, ICakeContext context) at Cake.Frosting.Internal.FrostingEngine`1.<>c__DisplayClass15_0.<ConfigureTasks>b__0(Exception exception) at Cake.Core.CakeTaskBuilderExtensions.<>c__DisplayClass15_0.<OnError>b__0(Exception exception, ICakeContext context) at Cake.Core.CakeTaskExtensions.<>c__DisplayClass3_0.<<SetErrorHandler>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Cake.Core.DefaultExecutionStrategy.HandleErrorsAsync(Func`3 action, Exception exception, ICakeContext context) at Cake.Core.CakeEngine.HandleErrorsAsync(IExecutionStrategy strategy, Func`3 errorHandler, Exception exception, ICakeContext context) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) at Cake.Core.CakeEngine.RunTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, String target, Stopwatch stopWatch, CakeReport report) at Cake.Core.CakeEngine.RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, ExecutionSettings settings) at Cake.Cli.BuildScriptHost`1.RunTargetAsync(String target) at Cake.Core.Scripting.ScriptHost.RunTarget(String target) at Cake.Frosting.Internal.FrostingEngine`1.Run(String target) at Cake.Frosting.Internal.DefaultCommand.Execute(CommandContext context, DefaultCommandSettings settings) > echo $LASTEXITCODE -1
The solution I found in the end was this, as this prints the correct stack trace, exits with a non-zero exit code.:
> .\FrostingTest.exe --target=some_task ======================================== some_task ======================================== An error occurred when executing task 'some_task'. Error: System.Exception: Oops at FrostingTest.SomeTask.Func2() in <redacted>:line 42 at FrostingTest.SomeTask.Func1() in <redacted>:line 37 at FrostingTest.SomeTask.Run(ICakeContext context) in <redacted>:line 32 at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.RunAsync(ICakeContext context) at Cake.Core.CakeTask.Execute(ICakeContext context) at Cake.Core.DefaultExecutionStrategy.ExecuteAsync(CakeTask task, ICakeContext context) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) at FrostingTest.SomeTask.OnError(Exception exception, ICakeContext context) in <redacted>:line 53 at Cake.Frosting.FrostingTask`1.Cake.Frosting.IFrostingTask.OnError(Exception exception, ICakeContext context) at Cake.Frosting.Internal.FrostingEngine`1.<>c__DisplayClass15_0.<ConfigureTasks>b__0(Exception exception) at Cake.Core.CakeTaskBuilderExtensions.<>c__DisplayClass15_0.<OnError>b__0(Exception exception, ICakeContext context) at Cake.Core.CakeTaskExtensions.<>c__DisplayClass3_0.<<SetErrorHandler>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Cake.Core.DefaultExecutionStrategy.HandleErrorsAsync(Func`3 action, Exception exception, ICakeContext context) at Cake.Core.CakeEngine.HandleErrorsAsync(IExecutionStrategy strategy, Func`3 errorHandler, Exception exception, ICakeContext context) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) at Cake.Core.CakeEngine.ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) at Cake.Core.CakeEngine.RunTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, String target, Stopwatch stopWatch, CakeReport report) at Cake.Core.CakeEngine.RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, ExecutionSettings settings) at Cake.Cli.BuildScriptHost`1.RunTargetAsync(String target) at Cake.Core.Scripting.ScriptHost.RunTarget(String target) at Cake.Frosting.Internal.FrostingEngine`1.Run(String target) at Cake.Frosting.Internal.DefaultCommand.Execute(CommandContext context, DefaultCommandSettings settings) > echo $LASTEXITCODE -1
What I don't like about this solution is, while it works, it means that all of my tasks either need to override OnError like this, or inherit from a base class that does. It also means not disposing an IDisposable returned from DiasnosticVerbosity (though I'm not sure how much I care about that since we Errored anyways). Is there some kind of configuration or something I can do to turn on to always print out the stack trace on an error case regardless of verbosity? Or is this the only solution there is? Admittedly, its not the end-of-the-world if there's not :P.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions