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

Add Error handler from within plumb #507

Closed
wants to merge 4 commits into from
Closed
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
19 changes: 18 additions & 1 deletion R/parse-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parseBlock <- function(lineNum, file){
paths <- NULL
preempt <- NULL
filter <- NULL
errorhandler <- NULL
image <- NULL
imageAttr <- NULL
serializer <- NULL
Expand Down Expand Up @@ -58,6 +59,18 @@ parseBlock <- function(lineNum, file){
filter <- f
}

errorMat <- stringi::stri_match(line, regex="^#['\\*]\\s*@errorhandler(\\s+(.*)$)?")
if (!is.na(errorMat[1,1])){
e <- stri_trim_both(errorMat[1,3])

if (!is.null(errorhandler)){
# Must have already assigned.
stopOnLine(lineNum, line, "Multiple @errorhandlers specified.")
}

errorhandler <- e
}

preemptMat <- stringi::stri_match(line, regex="^#['\\*]\\s*@preempt(\\s+(.*)\\s*$)?")
if (!is.na(preemptMat[1,1])){
p <- stri_trim_both(preemptMat[1,3])
Expand Down Expand Up @@ -222,6 +235,7 @@ parseBlock <- function(lineNum, file){
paths = paths,
preempt = preempt,
filter = filter,
errorhandler = errorhandler,
image = image,
imageAttr = imageAttr,
serializer = serializer,
Expand All @@ -236,7 +250,7 @@ parseBlock <- function(lineNum, file){
#' Evaluate and activate a "block" of code found in a plumber API file.
#' @include images.R
#' @noRd
evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, mount) {
evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, setErrorHandler, mount) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need anymore since #568.

lineNum <- srcref[1] - 1

block <- parseBlock(lineNum, file)
Expand Down Expand Up @@ -273,6 +287,9 @@ evaluateBlock <- function(srcref, file, expr, envir, addEndpoint, addFilter, mou
filter <- PlumberFilter$new(block$filter, expr, envir, block$serializer, srcref)
addFilter(filter)

} else if (!is.null(block$errorhandler)){
setErrorHandler(eval(expr))
Copy link
Collaborator

@meztez meztez Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the plumberRouter is now passed to evaluateBlock, use pr$setErrorHandler directly


} else if (!is.null(block$assets)){
path <- block$assets$path

Expand Down
2 changes: 1 addition & 1 deletion R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ plumber <- R6Class(
srcref <- attr(e, "srcref")[[1]][c(1,3)]

evaluateBlock(srcref, private$lines, e, private$envir, private$addEndpointInternal,
private$addFilterInternal, self$mount)
private$addFilterInternal, self$setErrorHandler, self$mount)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need since #568.

}

private$globalSettings <- parseGlobals(private$lines)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/files/errorhandler.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#* @errorhandler
function(req, res, err){
stop("Caught")
}

#* @get /fail
function() {
stop("fail")
return("foo")
}
9 changes: 9 additions & 0 deletions tests/testthat/test-errorhandler.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
context("errorHandler")

test_that("errorHandler works", {
res <- PlumberResponse$new("")

r <- plumber$new(test_path("files/errorhandler.R"))
expect_error(r$serve(make_req("GET", "/fail"), res),
regexp = "Caught")
})
4 changes: 3 additions & 1 deletion tests/testthat/test-parse-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ test_that("parseBlock works", {
"#' @get /",
"#' @post /",
"#' @filter test",
"#' @serializer json")
"#' @serializer json",
"#' @errorhandler\nfunction(){x}")
b <- parseBlock(length(lines), lines)
expect_length(b$path, 2)
expect_equal(b$path[[1]], list(verb="POST", path="/"))
expect_equal(b$path[[2]], list(verb="GET", path="/"))
expect_equal(b$filter, "test")
expect_equal(b$errorhandler, "function(){x}")
expect_equal_functions(b$serializer, serializer_json())
})

Expand Down