Skip to contents

Differentiate between client (4xx) and server (5xx) errors. Provides a mechanism to return custom status codes in combination with http_error() and http_success().

Usage

http_try(req, res, expr, silent = TRUE, ...)

http_try_handler(req, res, x)

Arguments

req

The request object.

res

The response object.

expr

An R expression to try.

silent

Logical, should the report of error messages be suppressed by try()?

...

Arguments passed to try()

x

The return value from try(expr).

Value

A list or the results from expr. A side effect is setting of the response status code on the response object and a log message to STDOUT or STDERR.

Details

If we catch an error:

  • the error is a clear server error coming from stop()

    • log it as an ERROR + print the error message to STDERR

    • return a generic status 500 message

    • set the status code of the response object to 500

  • the error is a structured HTTP error coming from http_error()

    • log it as an ERROR with the message from the condition attribute

    • return the specific HTTP error code with the structured output

    • set the status code of the response object

If we don't catch an error:

  • the object is of class http_success() (this comes in handy for async jobs and redirects)

    • log it as a SUCCESS with the message element

    • return the specific HTTP status code with the structured output

    • set the status code of the response object

  • the object is NOT of class http_success()

    • log it as a SUCCESS with a generic 200 message

    • return the object as is (default status code 200 assumed)

See also

Examples

req <- new.env()
res <- new.env()
http_try(req, res)
#> $category
#> [x] "Server Error"
#> 
#> $status
#> [x] 500
#> 
#> $message
#> [x] "Internal Server Error"
#> 
res$status
#> [1] 500

req <- new.env()
res <- new.env()
http_try(req, res, { 2 + 2 })
#> 7382 | 2024-05-30 07:19:38.344 [SUCCESS] Status 200: OK
#> [1] 4
res$status
#> NULL

req <- new.env()
res <- new.env()
http_try(req, res, http_error(401))
#> $category
#> [x] "Client Error"
#> 
#> $status
#> [x] 401
#> 
#> $message
#> [x] "Unauthorized"
#> 
res$status
#> [x] 401

req <- new.env()
res <- new.env()
http_try(req, res, http_success(201))
#> 7382 | 2024-05-30 07:19:38.353 [SUCCESS] Status 201: Created
#> $category
#> [x] "Successful"
#> 
#> $status
#> [x] 201
#> 
#> $message
#> [x] "Created"
#> 
res$status
#> [x] 201

req <- new.env()
res <- new.env()
http_try(req, res, { lm(NULL) })
#> $category
#> [x] "Server Error"
#> 
#> $status
#> [x] 500
#> 
#> $message
#> [x] "Internal Server Error"
#> 
res$status
#> [1] 500

req <- new.env()
res <- new.env()
http_try(req, res, { stop("Stop!!!") })
#> $category
#> [x] "Server Error"
#> 
#> $status
#> [x] 500
#> 
#> $message
#> [x] "Internal Server Error"
#> 
res$status
#> [1] 500

req <- new.env()
res <- new.env()
f <- function() stop("Stop!!!")
http_try(req, res, { f() })
#> $category
#> [x] "Server Error"
#> 
#> $status
#> [x] 500
#> 
#> $message
#> [x] "Internal Server Error"
#> 
res$status
#> [1] 500

req <- new.env()
res <- new.env()
http_try_handler(req, res, { try(f()) })
#> Error in f() : Stop!!!
#> $category
#> [x] "Server Error"
#> 
#> $status
#> [x] 500
#> 
#> $message
#> [x] "Internal Server Error"
#> 
res$status
#> [1] 500