Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

with_abort

Promote all errors to rlang errors


Description

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

Usage

with_abort(expr, classes = "error")

Arguments

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.

Details

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.

Examples

# with_abort() automatically casts simple errors thrown by stop()
# to rlang errors. It is is handy for rethrowing low level
# errors. The backtraces are then segmented between the low level
# and high level contexts.
f <- function() g()
g <- function() stop("Low level error")

high_level <- function() {
  with_handlers(
    with_abort(f()),
    error = ~ abort("High level error", parent = .)
  )
}

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.