-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opt-In Panic Recovery #364
Conversation
Codecov Report
@@ Coverage Diff @@
## master #364 +/- ##
==========================================
+ Coverage 98.30% 98.33% +0.03%
==========================================
Files 21 21
Lines 1414 1441 +27
==========================================
+ Hits 1390 1417 +27
Misses 15 15
Partials 9 9
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
posting on behalf of @r-hang:
Perhaps add some more details on how to use this in the PR description.
Since uber-go/dig#364 was merged into Dig, we can now built upon it to add the ability to recover from panics in user-provided code into Fx, by providing a `fx.RecoverFromPanics()` option into their application. This is disallowed on the module level, only on the application level. ```go app := fx.New( fx.Provide(func() int { panic("terrible sorrow") }), fx.Invoke(func(a int) {}), fx.RecoverFromPanics(), // panic will be set to app.Err now ) err := app.Err() var pe dig.PanicError if errors.As(err, &pe) { // panic happened when we invoke // handle panic } ``` Co-authored-by: Sung Yoon Whang <sungyoon@uber.com> Co-authored-by: Sung Yoon Whang <sungyoonwhang@gmail.com>
Currently, when a panic occurs in provided/invoked/decorated functions, the message users see can be very unwieldy, especially with complicated dependency graphs.
This change allows us to construct our container with a new
Option
calledRecoverFromPanics()
:When this is done, any panics that occur in provided/invoked/decorated functions will be recovered from, and wrapped in a new error Type,
PanicError
, containing the relevant function and the panic value. ThisPanicError
will then be dealt with as a normal error when propagated back us, meaning panics would be just as readable as normal errors when dealing with long complicated call chains.When opted in, we can distinguish panics from dig errors and non-dig errors when calling
c.Invoke
like so:Or, if we just want to distinguish panics from errors we can simply check
errors.As(err, &pe)
.