Skip to contents

Evaluates the given expression and then runs the on_success, on_error, or on_warning expressions depending on the result of the initial evaluation.

Usage

handle_error(
  expr,
  on_success = {
 },
  on_error = {
 },
  on_warning = {
 },
  finally = {
 }
)

Arguments

expr

Expression to evaluate

on_success

Expression to evaluate if expr succeeds. Evaluated after expr completes; conditions it signals are not caught by on_error or on_warning.

on_error

Expression to evaluate if expr generates an error

on_warning

Expression to evaluate if expr generates a warning

finally

Expression to evaluate after expr and on_success or on_error or on_warning, usually used for cleanup.

Value

Invisible result of expr if it succeeds, otherwise the error or warning object.

Examples

handle_error(
  1 + 1,
  on_success = message("success"),
  on_error = message("error")
)
#> success

handle_error(
  stop("Something went wrong"),
  on_success = message("success"),
  on_error = message("error")
)
#> error