feat: Add onUnhandledError()
#3993
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a re-working of #3989.
Using an option like
shiny.error.unhandled
is indicative of where John and I initially started trying to solve the problem by extending theshiny.error
option. But in retrospect, given that the final solution was added tosession$unhandledError()
, the registration mechanism of using an option no longer makes as much sense.This PR replaces the global option with
onUnhandledError()
, a function that registers the unhandled error callback in the same way asonStop()
oronSessionEnded()
. It's documented inonFlush()
because those docs include "callbacks on Shiny session events" and the unhandled error is basically a session (-ending) event.The biggest advantage of this approach is that global and session-level callbacks can be registered independently and without having to do a lot of additional work to chain rather than replace existing callbacks. (Also this approach is more consistent with nearby methods.)
What is an unhandled error?
Another thing that bugged me about the previous version is that we were considering only fatal errors to be "unhandled". But unanticipated errors in side a
render____()
function still result in printed errors in logs and show up in the UI of a running app. It's equally difficult to write code to catch these errors.I've updated this PR to also call
$unhandledError()
when these types of errors occur. This gives us a chance to run the unhandled error callbacks. To achieve this, two changes were made:unHandledError()
gains aclose
argument that determines if the session should be closed (TRUE
by default).shiny.error.fatal
class so that error handlers can differentiate between fatal and non-fatal unanticipated errors.The example code below includes an example of both and I added a section in the docs to focus on
onUnhandledError()
usage.Example