R: Promote all errors to rlang errors

with_abort {rlang} | R Documentation

## 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 = .)
      )
    }

[Package rlang version 0.4.2 Index]