| with_abort {rlang} | R Documentation |
with_abort() promotes conditions as if they were thrown with
abort(). These errors embed a backtrace. They are
particularly suitable to be set as parent errors (see parent
argument of abort()).
with_abort(expr, classes = "error")
expr |
An expression run in a context where errors are promoted to rlang errors. |
classes |
Character vector of condition classes that should be promoted to rlang errors. |
with_abort() installs a calling handler for errors and
rethrows non-rlang errors with abort(). However, error handlers
installed within with_abort() have priority. For this reason,
you should use tryCatch() and exiting handlers outside
with_abort() rather than inside.
# For cleaner backtraces:
options(rlang_trace_top_env = current_env())
# with_abort() automatically casts simple errors thrown by stop()
# to rlang errors:
fn <- function() stop("Base error")
try(with_abort(fn()))
last_error()
# with_abort() is handy for rethrowing low level errors. The
# backtraces are then segmented between the low level and high
# level contexts.
low_level1 <- function() low_level2()
low_level2 <- function() stop("Low level error")
high_level <- function() {
with_handlers(
with_abort(low_level1()),
error = ~ abort("High level error", parent = .x)
)
}
try(high_level())
last_error()
summary(last_error())
# Reset to default
options(rlang_trace_top_env = NULL)