abort | R Documentation |
These functions are equivalent to base functions base::stop()
,
base::warning()
, and base::message()
. They signal a condition
(an error, warning, or message respectively) and make it easy to
supply condition metadata:
Supply class
to create a classed condition that can be caught
or handled selectively, allowing for finer-grained error
handling.
Supply metadata with named ...
arguments. This data is stored
in the condition object and can be examined by handlers.
Supply call
to inform users about which function the error
occurred in.
Supply another condition as parent
to create a chained condition.
Certain components of condition messages are formatted with unicode symbols and terminal colours by default. These aspects can be customised, see Customising condition messages.
abort(
message = NULL,
class = NULL,
...,
call,
body = NULL,
footer = NULL,
trace = NULL,
parent = NULL,
use_cli_format = NULL,
.inherit = TRUE,
.internal = FALSE,
.file = NULL,
.frame = caller_env(),
.trace_bottom = NULL,
.subclass = deprecated()
)
warn(
message = NULL,
class = NULL,
...,
body = NULL,
footer = NULL,
parent = NULL,
use_cli_format = NULL,
.inherit = NULL,
.frequency = c("always", "regularly", "once"),
.frequency_id = NULL,
.subclass = deprecated()
)
inform(
message = NULL,
class = NULL,
...,
body = NULL,
footer = NULL,
parent = NULL,
use_cli_format = NULL,
.inherit = NULL,
.file = NULL,
.frequency = c("always", "regularly", "once"),
.frequency_id = NULL,
.subclass = deprecated()
)
signal(message = "", class, ..., .subclass = deprecated())
reset_warning_verbosity(id)
reset_message_verbosity(id)
message |
The message to display, formatted as a bulleted
list. The first element is displayed as an alert bullet
prefixed with If a message is not supplied, it is expected that the message is
generated lazily through If a function, it is stored in the |
class |
Subclass of the condition. |
... |
Additional data to be stored in the condition object.
If you supply condition fields, you should usually provide a
|
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
body , footer |
Additional bullets. |
trace |
A |
parent |
Supply
For more information about error calls, see Including contextual information with error chains. |
use_cli_format |
Whether to format If set to |
.inherit |
Whether the condition inherits from |
.internal |
If |
.file |
A connection or a string specifying where to print the
message. The default depends on the context, see the |
.frame |
The throwing context. Used as default for
|
.trace_bottom |
Used in the display of simplified backtraces
as the last relevant call frame to show. This way, the irrelevant
parts of backtraces corresponding to condition handling
( |
.subclass |
This argument
was renamed to |
.frequency |
How frequently should the warning or message be
displayed? By default ( |
.frequency_id |
A unique identifier for the warning or
message. This is used when |
id |
The identifying string of the condition that was supplied
as |
abort()
throws subclassed errors, see
"rlang_error"
.
warn()
temporarily set the warning.length
global option to
the maximum value (8170), unless that option has been changed
from the default value. The default limit (1000 characters) is
especially easy to hit when the message contains a lot of ANSI
escapes, as created by the crayon or cli packages
As with base::stop()
, errors thrown with abort()
are prefixed
with "Error: "
. Calls and source references are included in the
prefix, e.g. "Error in
my_function() at myfile.R:1:2:"
. There
are a few cosmetic differences:
The call is stripped from its arguments to keep it simple. It is then formatted using the cli package if available.
A line break between the prefix and the message when the former is too long. When a source location is included, a line break is always inserted.
If your throwing code is highly structured, you may have to
explicitly inform abort()
about the relevant user-facing call to
include in the prefix. Internal helpers are rarely relevant to end
users. See the call
argument of abort()
.
abort()
saves a backtrace in the trace
component of the error
condition. You can print a simplified backtrace of the last error
by calling last_error()
and a full backtrace with
summary(last_error())
. Learn how to control what is displayed
when an error is thrown with rlang_backtrace_on_error
.
Signalling a condition with inform()
or warn()
displays a
message in the console. These messages can be muffled as usual with
base::suppressMessages()
or base::suppressWarnings()
.
inform()
and warn()
messages can also be silenced with the
global options rlib_message_verbosity
and
rlib_warning_verbosity
. These options take the values:
"default"
: Verbose unless the .frequency
argument is supplied.
"verbose"
: Always verbose.
"quiet"
: Always quiet.
When set to quiet, the message is not displayed and the condition is not signalled.
stdout
and stderr
By default, abort()
and inform()
print to standard output in
interactive sessions. This allows rlang to be in control of the
appearance of messages in IDEs like RStudio.
There are two situations where messages are streamed to stderr
:
In non-interactive sessions, messages are streamed to standard
error so that R scripts can easily filter them out from normal
output by redirecting stderr
.
If a sink is active (either on output or on messages) messages
are always streamd to stderr
.
These exceptions ensure consistency of behaviour in interactive and non-interactive sessions, and when sinks are active.
Including function calls in error messages
Including contextual information with error chains
# These examples are guarded to avoid throwing errors
if (FALSE) {
# Signal an error with a message just like stop():
abort("The error message.")
# Unhandled errors are saved automatically by `abort()` and can be
# retrieved with `last_error()`. The error prints with a simplified
# backtrace:
f <- function() try(g())
g <- function() evalq(h())
h <- function() abort("Tilt.")
last_error()
# Use `summary()` to print the full backtrace and the condition fields:
summary(last_error())
# Give a class to the error:
abort("The error message", "mypkg_bad_error")
# This allows callers to handle the error selectively
tryCatch(
mypkg_function(),
mypkg_bad_error = function(err) {
warn(conditionMessage(err)) # Demote the error to a warning
NA # Return an alternative value
}
)
# You can also specify metadata that will be stored in the condition:
abort("The error message.", "mypkg_bad_error", data = 1:10)
# This data can then be consulted by user handlers:
tryCatch(
mypkg_function(),
mypkg_bad_error = function(err) {
# Compute an alternative return value with the data:
recover_error(err$data)
}
)
# If you call low-level APIs it may be a good idea to create a
# chained error with the low-level error wrapped in a more
# user-friendly error. Use `try_fetch()` to fetch errors of a given
# class and rethrow them with the `parent` argument of `abort()`:
file <- "http://foo.bar/baz"
try(
try_fetch(
download(file),
error = function(err) {
msg <- sprintf("Can't download `%s`", file)
abort(msg, parent = err)
})
)
# You can also hard-code the call when it's not easy to
# forward it from the caller
f <- function() {
abort("my message", call = call("my_function"))
}
g <- function() {
f()
}
# Shows that the error occured in `my_function()`
try(g())
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.