http-try {tryr}R Documentation

Client/Server Error Handling

Description

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).

Details

If we catch an error:

If we don't catch an error:

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.

See Also

try(), msg()

Examples

req <- new.env()
res <- new.env()

http_try(req, res)
res$status

http_try(req, res, { 2 + 2 })
res$status

http_try(req, res, http_error(401))
res$status

http_try(req, res, http_success(201))
res$status

http_try(req, res, { lm(NULL) })
res$status

http_try(req, res, { stop("Stop!!!") })
res$status


f <- function() stop("Stop!!!")
http_try(req, res, { f() })
res$status

http_try_handler(req, res, { try(f()) })
res$status


[Package tryr version 0.1.1 Index]