Skip to content
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

Force error handler to serialize with unboxed json serializer #689

Merged
merged 9 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
plumber 1.0.0.9999 Development version
--------------------------------------------------------------------------------

### Breaking changes

* Force json serialization of endpoint error responses instead of using endpoint serializer. (@meztez, #689)

### New features

* Passing `edit = TRUE` to `plumb_api()` will open the API source file (#699)
Expand All @@ -9,10 +13,11 @@ plumber 1.0.0.9999 Development version

### Bug fixes

* When calling `Plumber$handle()` and defining a new `PlumberEndpoint`, `...` will be checked for invalid names #677
* When calling `Plumber$handle()` and defining a new `PlumberEndpoint`, `...` will be checked for invalid names (@meztez, #677)

* `/__swagger__/` now always redirect to `/__docs__/`, even when Swagger isn't the selected interface. Use `options(plumber.legacyRedirects = FALSE)` to disable this behavior (@blairj09 #694)


plumber 1.0.0
--------------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions R/default-handlers.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ default404Handler <- function(req, res){
return(list(error = "405 - Method Not Allowed"))
}
res$status <- 404
res$serializer <- serializer_unboxed_json()
list(error="404 - Resource Not Found")
}

Expand All @@ -20,6 +21,7 @@ defaultErrorHandler <- function(){
# It's possible, however, than a handler set a 40x and then wants to use this function to
# render an error, though.
res$status <- 500
res$serializer <- serializer_unboxed_json()
meztez marked this conversation as resolved.
Show resolved Hide resolved
li$error <- "500 - Internal server error"
} else {
li$error <- "Internal error"
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/helper-mock-request.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

make_req <- function(verb = "GET", path = "/", qs="", body="", args = c(), ...){
make_req <- function(verb = "GET", path = "/", qs="", body="", args = c(), pr = NULL, ...){
req <- as.environment(list(...))
req$REQUEST_METHOD <- toupper(verb)
req$PATH_INFO <- path
Expand All @@ -13,5 +13,6 @@ make_req <- function(verb = "GET", path = "/", qs="", body="", args = c(), ...){
read = function(){ body },
rewind = function(){ length(body) })
req$bodyRaw <- body
req$pr <- pr
req
}
3 changes: 3 additions & 0 deletions tests/testthat/test-async.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ test_that("async hooks change value being passed through", {
context("Promise - errors are handled")

expect_route_error <- function(response, txt) {
if (!is.list(response$body)) {
response$body <- jsonlite::parse_json(response$body, simplifyVector = TRUE)
}
expect_equal(response$body$error, "500 - Internal server error")
expect_true(grepl(txt, response$body$message))
}
Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/test-endpoint-aroundexec.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ test_that("not producing an image produces an error", {
})

expect_output(
result <- root$call(make_req("GET", "/no_plot")),
result <- root$call(make_req("GET", "/no_plot", pr = root)),
"device output file is missing"
)
expect_equal(result$status, 500)
if (!is.list(result$body)) {
result$body <- jsonlite::parse_json(result$body, simplifyVector = TRUE)
}
expect_true(grepl("device output file is missing", result$body$message))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-serializer-feather.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ test_that("Errors call error handler", {
serializer_feather()(parse(text="hi"), data.frame(), PlumberResponse$new("csv"), errorHandler = errHandler)
expect_equal(errors, 1)
})

test_that("Errors are rendered correctly with debug TRUE", {
skip_if_not_installed("feather")

pr <- pr() %>% pr_get("/", function() stop("myerror"), serializer = serializer_feather()) %>% pr_set_debug(TRUE)
capture.output(res <- pr$serve(make_req(pr = pr), PlumberResponse$new("csv")))

expect_match(res$body, "Error in (function () : myerror", fixed = TRUE)
})