Skip to contents

Error class

Arguments

behavior

Behavior of the error. default: auto. See Details

message_template

A message template. optional. use whisker templating. names to use are: reason and status. use in template like {{reason}} and {{status}}. Note that {{message}} that is used in message_template_verbose will be ignored here.

call.

(logical) indicating if the call should become part of the error message. Default: FALSE

message_template_verbose

A verbose message template. optional. use whisker templating. names to use are: reason, status, message. use in template like {{reason}}, {{status}}, and {{message}}. Note that this is ignored here, but is used in the HTTP* methods (e.g. HTTPBadRequest)

muffle

(logical) whether to not respond when status codes in 1xx-3xx series. Default: FALSE

Details

Methods

  • do(response, mssg) Execute condition, whether it be message, warning, or error.

    • response: is any response from crul, curl, or httr Execute condition, whether it be message, warning, error, or your own custom function. This method uses message_template_verbose, and uses it's default value.

    • mssg: character string message to include in call. ignored if template does not have a message entry

  • set_behavior(behavior)

    Set behavior, same as setting behavior on initializing with $new()

behavior parameter options

  • stop - use stop

  • warning - use warning

  • message - use message

  • auto - toggle between stop and message depending on the HTTP status code series. Defaults will be:

    • 1xx: message

    • 2xx: message

    • 3xx: message

    • 4xx: stop

    • 5xx: stop

Of course, you can always override the defaults.

See also

Examples

Error$new()
#> <HTTP Error>
#>   behavior: stop
#>   message_template: {{reason}} (HTTP {{status}})
#>   message_template_verbose: {{reason}} (HTTP {{status}}).\n - {{message}}
# reset behavior
(z <- Error$new())
#> <HTTP Error>
#>   behavior: stop
#>   message_template: {{reason}} (HTTP {{status}})
#>   message_template_verbose: {{reason}} (HTTP {{status}}).\n - {{message}}
z$set_behavior("warning")
z
#> <HTTP Error>
#>   behavior: warning
#>   message_template: {{reason}} (HTTP {{status}})
#>   message_template_verbose: {{reason}} (HTTP {{status}}).\n - {{message}}

if (requireNamespace("crul")) {
 library("crul")
 res <- HttpClient$new("https://httpbin.org/status/418")$get()

 # stop
 (x <- Error$new(behavior = "stop"))
 if (FALSE) x$do(res)

 # warn
 (x <- Error$new(behavior = "warning"))
 x$do(res)

 # do vs. do_verbose
 x <- HTTPRequestURITooLong$new(behavior = "stop")
 res <- HttpClient$new("https://httpbin.org/status/414")$get()
 if (FALSE) {
 http414(res)
 ## with template
 http414(res, message_template = "{{status}}\n  --> {{reason}}")
 x$do(res)
 x$do_verbose(res)
 }

 # service unavailable
 x <- HTTPServiceUnavailable$new(behavior = "stop")
 res <- HttpClient$new("https://httpbin.org/status/503")$get()
 if (FALSE) {
 x$do(res)
 x$do_verbose(res)
 }

 # message template
 y <- Error$new(message_template = "{{reason}} ............ {{status}}")
 res <- HttpClient$new("https://httpbin.org/status/418")$get()
 if (FALSE) {
 y$do(res)
 }

 yy <- Error$new(message_template = "{{status}}\n  --> {{reason}}")
 yy$message_template
 if (FALSE) {
 yy$do(res)
 }

 ## with verbose message
 library(crul)
 res <- HttpClient$new("https://httpbin.org/status/401")$get()
 yy <- HTTPUnauthorized$new()
 zz <- HTTPUnauthorized$new(
   message_template = "HTTP({{status}}): {{reason}}\n  {{message}}"
 )
 yy$message_template; zz$message_template
 if (FALSE) {
 yy$do(res)
 zz$do(res)
 yy$do_verbose(res)
 zz$do_verbose(res)
 }

 yy <- Error$new(
   message_template = "HTTP({{status}}): {{reason}}\n  {{message}}"
 )
 yy$message_template
 if (FALSE) yy$do(res)

 # muffle responses
 (x <- Error$new(muffle = TRUE))
 res <- crul::HttpClient$new("https://httpbin.org/status/226")$get()
 z <- x$do(res)
 z
}
#> Warning: I'm a teapot (HTTP 418)
#> <crul response> 
#>   url: https://httpbin.org/status/226
#>   request_headers: 
#>     User-Agent: libcurl/7.81.0 r-curl/4.3.2 crul/1.2.1.99
#>     Accept-Encoding: gzip, deflate
#>     Accept: application/json, text/xml, application/xml, */*
#>   response_headers: 
#>     status: HTTP/2 226
#>     date: Mon, 25 Jul 2022 06:43:21 GMT
#>     content-type: text/html; charset=utf-8
#>     content-length: 0
#>     server: gunicorn/19.9.0
#>     access-control-allow-origin: *
#>     access-control-allow-credentials: true
#>   status: 226