abort()
and error_cnd()
create errors of class "rlang_error"
.
The differences with base errors are:
Implementing
conditionMessage()
methods for subclasses of"rlang_error"
is undefined behaviour. Instead, implement thecnd_header()
method (and possiblycnd_body()
andcnd_footer()
). These methods return character vectors which are assembled by rlang when needed: whenconditionMessage.rlang_error()
is called (e.g. viatry()
), when the error is displayed throughprint()
orformat()
, and of course when the error is displayed to the user byabort()
.cnd_header()
,cnd_body()
, andcnd_footer()
methods can be overridden by storing closures in theheader
,body
, andfooter
fields of the condition. This is useful to lazily generate messages based on state captured in the closure environment.The
use_cli_format
condition field instructs whether to use cli (or rlang's fallback method if cli is not installed) to format the error message at print time.In this case, the
message
field may be a character vector of header and bullets. These are formatted at the last moment to take the context into account (starting position on the screen and indentation).See
local_use_cli()
for automatically setting this field in errors thrown withabort()
within your package.