These constructors make it easy to create subclassed conditions. Conditions are objects that power the error system in R. They can also be used for passing messages to pre-established handlers.

error_cnd(.subclass = NULL, ..., message = "", trace = NULL,
parent = NULL)

cnd(class, ..., message = "", .subclass)

warning_cnd(class = NULL, ..., message = "", .subclass)

message_cnd(class = NULL, ..., message = "", .subclass)

## Arguments

.subclass This argument was renamed to class in rlang 0.4.2. It will be deprecated in the next major version. This is for consistency with our conventions for class constructors documented in https://adv-r.hadley.nz/s3.html#s3-subclassing. Named data fields stored inside the condition object. A default message to inform the user about the condition when it is signalled. A trace object created by trace_back(). A parent condition object created by abort(). The condition subclass.

## Details

cnd() creates objects inheriting from condition. Conditions created with error_cnd(), warning_cnd() and message_cnd() inherit from error, warning or message.

## Lifecycle

The .type and .msg arguments have been renamed to .subclass and message. They are deprecated as of rlang 0.3.0.

## See also

cnd_signal(), with_handlers().

## Examples

# Create a condition inheriting from the s3 type "foo":
cnd <- cnd("foo")

# Signal the condition to potential handlers. Since this is a bare
# condition the signal has no effect if no handlers are set up:
cnd_signal(cnd)

# When a relevant handler is set up, the signal causes the handler
# to be called:
with_handlers(cnd_signal(cnd), foo = exiting(function(c) "caught!"))#> Warning: exiting() is soft-deprecated as of rlang 0.4.0.
#> Handlers are now treated as exiting by default.
#> This warning is displayed once per session.#> [1] "caught!"
# Handlers can be thrown or executed inplace. See with_handlers()
# documentation for more on this.

# Signalling an error condition aborts the current computation:
err <- error_cnd("foo", message = "I am an error")
try(cnd_signal(err))#> Error : I am an error