Skip to content

Write a condition as a DataExcept envelope

Source: R/envelope.R ยท Help: ?condition_to_envelope

condition_to_envelope() renders a condition, and the chain of conditions behind it, as a DataExcept envelope: the language-neutral failure record the Python DataExcept package defines in its published JSON Schema (envelope_schema()). condition_to_json() encodes it as strict JSON.

Usage

condition_to_envelope(cnd, include_attributes = TRUE, max_depth = 8L)

condition_to_json(
  cnd,
  include_attributes = TRUE,
  max_depth = 8L,
  pretty = FALSE
)

condition_type(cnd)

Arguments

cnd
A condition object.
include_attributes
Include the attributes field?
max_depth
How many levels of chained conditions to render. A cause beyond this depth is replaced by the truncation marker {"truncated": true}.
pretty
Indent the JSON for reading?

Value

condition_to_envelope() returns the envelope as a list (JSON objects as named lists, arrays as unnamed lists, null as NULL). condition_to_json() returns a single string.

condition_type() returns the type a condition is written with: the dataexcept type when there is one -- including the type of a condition read from an envelope -- and otherwise the condition's most specific R class.

Details

Any condition can be written, not only dataexcept ones:

  • type is the dataexcept type, or else the condition's most specific R class (simpleError, rlang_error, ...).
  • module is "dataexcept" for dataexcept conditions, "base" for base R's own condition classes, the package for a class named with its package prefix (rlang_error belongs to rlang), and "unknown" where the package cannot be established.
  • message is the condition message.
  • failure appears on dataexcept errors only. A condition dataexcept did not classify has no failure field, and that absence is information: it means unclassified, not "not retryable".
  • attributes holds the condition's fields, in a JSON-safe form. The message, call, parent and backtrace are not attributes, nor is any field whose name begins with . or _.
  • cause is the condition in the parent field -- the field dataexcept and rlang both use for a chained cause -- rendered the same way.

Text is redacted on the way out: every URL in the message, in attribute values and in attribute names loses its credentials and its path.

A vector of length one is written as a scalar. Wrap a field in I() when it is a list by meaning, so that it is written as an array even with a single element -- the convention jsonlite uses. The constructors do this for the fields the Python package always writes as lists, such as the expected types of dtype_mismatch_error().

Values that JSON cannot represent degrade to a description of themselves rather than disappearing: a data frame becomes "<data.frame: 150 rows x 5 columns>", a function "<function>", a vector longer than 100 elements a description of its type and length, and NaN or Inf the strings "nan" or "inf", as the Python package writes them. NA becomes null.

Examples

cnd <- tryCatch(
  stop(missing_column_error("customer_id", dataframe = "orders")),
  error = identity
)
cat(condition_to_json(cnd, pretty = TRUE))

# A chain: the base R error becomes the cause.
cnd <- tryCatch(
  tryCatch(
    read.csv(tempfile()),
    warning = function(w) stop(file_read_error("orders.csv", parent = w))
  ),
  error = identity
)
str(condition_to_envelope(cnd), max.level = 2)

See also

envelope_to_condition() for the reverse direction.