Skip to content

Read a DataExcept envelope back into an R condition

Source: R/read-envelope.R ยท Help: ?envelope_to_condition

envelope_to_condition() turns an envelope -- written by this package or by the Python DataExcept package, or by any producer that follows the published schema -- into an R condition that can be inspected, handled or re-signalled.

Usage

envelope_to_condition(x, max_depth = 32L)

Arguments

x
An envelope: a JSON string, or a list as returned by condition_to_envelope() or jsonlite::parse_json().
max_depth
The deepest chain of nested records to accept.

Value

A condition object.

Details

The condition always has class dataexcept_remote_condition, plus error (or warning, for a warning written by this package) and condition. When the envelope names a dataexcept type, the condition also gets that type's R classes, whichever language wrote it: a Python MissingColumnError becomes a dataexcept_missing_column_error, so an R handler written for local failures catches remote ones too. A type from a DataExcept module that R has no class for, such as Python's ServiceTimeoutError, is still a dataexcept_error, since every DataExcept exception derives from DataExceptError; condition_type() gives its type.

The envelope's attributes become fields (cnd$column), except any that would collide with message, call or parent. Its cause becomes parent, so the chain can be walked and printed as usual.

Written back with condition_to_envelope(), a condition read here reproduces the envelope it came from, including its attributes, failure record, context, group members and cycle and truncation markers.

Envelopes may come from outside the process, so reading is bounded: a chain of causes, contexts or group members nested more than max_depth levels deep is rejected before it is walked, and JSON text nested deeper than the envelope could legitimately need is rejected before it is parsed. Envelopes written with either package's default depth (8) are far inside the limit.

Examples

# An envelope written by the Python package, one of the reference
# fixtures shipped with dataexcept.
path <- system.file("schema", "fixtures", "ordinary-exception.json", package = "dataexcept")
json <- paste(readLines(path), collapse = "\n")
cat(json)

cnd <- envelope_to_condition(json)
class(cnd)
cnd$field
is_retryable(cnd)

tryCatch(stop(cnd), dataexcept_validation_error = function(e) "caught")

See also

validate_envelope(), which this function applies first.