Skip to content

feat: Add onUnhandledError() #3993

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

Merged
merged 8 commits into from
Mar 14, 2024
Merged

feat: Add onUnhandledError() #3993

merged 8 commits into from
Mar 14, 2024

Conversation

gadenbuie
Copy link
Member

@gadenbuie gadenbuie commented Mar 11, 2024

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 the shiny.error option. But in retrospect, given that the final solution was added to session$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 as onStop() or onSessionEnded(). It's documented in onFlush() 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:

  1. unHandledError() gains a close argument that determines if the session should be closed (TRUE by default).
  2. When a fatal error occurs, we add the 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

library(shiny)

ui <- fixedPage(
  markdown(c(
    "Set the number to 8 or higher to cause an error", 
    "in the `renderText()` output."
  )),
  sliderInput("number", "Number", 0, 10, 4),
  textOutput("text"),
  hr(),
  markdown(c(
    "Click the button below to crash the app with an unhandled error",
    "in an `observe()` block."
  )),
  actionButton("crash", "Crash the app!")
)

log_event <- function(level, ...) {
  ts <- strftime(Sys.time(), " [%F %T] ")
  message(level, ts, ...)
}

server <- function(input, output, session) {
  log_event("INFO", "Session started")
  
  onUnhandledError(function(err) {
    # log the unhandled error
    level <- if (inherits(err, "shiny.error.fatal")) "FATAL" else "ERROR"
    log_event(level, conditionMessage(err))
  })

  onStop(function() {
    log_event("INFO", "Session ended")
  })

  observeEvent(input$crash, stop("Oops, an unhandled error happened!"))
  
  output$text <- renderText({
    if (input$number > 7) {
      stop("that's too high!")
    }
    sprintf("You picked number %d.", input$number)
  })
}

shinyApp(ui, server)

@gadenbuie gadenbuie marked this pull request as ready for review March 11, 2024 14:52
@gadenbuie gadenbuie requested a review from wch March 11, 2024 14:53
@gadenbuie gadenbuie added this to the 1.9 milestone Mar 11, 2024
@gadenbuie gadenbuie merged commit 3c4a908 into main Mar 14, 2024
@gadenbuie gadenbuie deleted the feat/onUnhandledError branch March 14, 2024 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants