rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.38k stars 256 forks source link

Should mounted routers inherit error handler function? #815

Open ARawles opened 2 years ago

ARawles commented 2 years ago

As displayed in this example:

mounted_api <- plumber::pr() %>%
  plumber::pr_get("/error", 
         function() {
           stop("mounted error")
         })

plumber::pr() %>%
  plumber::pr_set_error(function(req, res, err) {
    "This error is from the handler"
  }) %>%
  plumber::pr_get("/error", function() {
    stop("unmounted error")
  }) %>%
  plumber::pr_mount("mounted", mounted_api) %>%
  plumber::pr_run()

The error raised in the base "/error" endpoint is passed to the error handler set via pr_set_error(), but the error raised in the mounted router via the "/mounted/error" isn't.

Is this the expected behaviour? I appreciate that this does allow different mounted routers to use different error handling functions, but also introduces some duplication when you want to use the same one. Could the mounted routers perhaps inherit their error path from the parent with the opportunity to set more specific error handlers like so:

mounted_api <- plumber::pr() %>%
  plumber::pr_get("/error", 
         function() {
           stop("mounted error")
         }) %>%
  plumber::pr_get("/error", function() {
    stop("unmounted error")
  })
LeeMendelowitz commented 3 months ago

I just stumbled upon this confusing behavior. An ideal setup would be to have the error handler in the base router handle any unhandled errors in any of it's mounted routers.

With the current implementation, if you are trying to compose a complex API and mounting several routers, then you have to put the error handlers in each mounted child router instead of just being able to set it once in the base router.