Skip to content

Create a dataexcept error

Source: R/condition.R ยท Help: ?new_dataexcept_error

new_dataexcept_error() is the constructor every dataexcept error is built with, and the way to define your own. It returns a condition object without signalling it; signal it with stop() (or rlang::abort()), exactly as you would a condition from errorCondition().

Usage

new_dataexcept_error(
  message,
  ...,
  type = "DataExceptError",
  class = character(),
  parent = NULL,
  call = NULL,
  failure = NULL
)

Arguments

message
The condition message, a single string.
...
Named fields to store on the condition.
type
The envelope type. A registered type (see dataexcept_classes()) supplies the R classes and the default failure metadata. An unregistered type is allowed for your own errors: pass the R class in class.
class
Additional R classes, most specific first. They are placed in front of the classes of type.
parent
The condition that caused this one, or NULL.
call
The call to report, or NULL. Pass sys.call() from inside your function to report its call.
failure
Failure metadata from failure_metadata(); defaults to the default of type.

Value

A condition object of class c(class, <classes of type>, "error", "condition").

Details

A dataexcept error is an ordinary R condition. Its fields are list elements, so a handler reads them directly (cnd$column); its classes are what tryCatch() matches; and its cause is stored in parent, the field rlang uses, so rlang prints the chain as "Caused by error".

Credentials never reach a dataexcept condition. URLs in the message and in character fields are redacted at construction: userinfo and credential query parameters are removed, while scheme, host and port stay, because they are what makes an error actionable.

Examples

# Your own error, still caught as a dataexcept error and serialisable to
# the envelope.
quota_error <- function(used, limit) {
  new_dataexcept_error(
    sprintf("Quota exceeded: %d of %d requests used", used, limit),
    used = used,
    limit = limit,
    type = "QuotaExceededError",
    class = "myapp_quota_exceeded_error",
    failure = failure_metadata("transient", retryable = TRUE)
  )
}

tryCatch(
  stop(quota_error(1000L, 1000L)),
  dataexcept_error = function(e) condition_to_json(e)
)

See also

The constructors for specific failures, such as missing_column_error() and validation_error().