R already has classed conditions. dataexcept adds a
shared vocabulary of failure classes, machine-readable recovery metadata
and a JSON envelope compatible with Python DataExcept. These examples
run locally without Python or an external service.
Each constructor returns a condition, which is signalled by
stop(). Handlers can catch a specific class or a broader
family.
library(dataexcept)
validate_orders <- function(orders) {
if (!"customer_id" %in% names(orders)) {
stop(missing_column_error("customer_id", dataframe = "orders"))
}
invisible(orders)
}
orders <- data.frame(amount = c(10, 20, 30))
caught <- tryCatch(
validate_orders(orders),
dataexcept_data_frame_error = identity
)
stopifnot(inherits(caught, "dataexcept_missing_column_error"))
conditionMessage(caught)
#> [1] "Missing required column 'customer_id' in data frame 'orders'"
caught$column
#> [1] "customer_id"The column name stays structured. Consumers need not parse an error message.
Failure metadata describes whether retrying might help. Scheduling retries remains the application’s responsibility. An unclassified failure retains unknown retryability rather than falsely claiming it is permanent.
remote_failure <- api_error(
"https://api.example.com/v1/orders?api_key=SECRET_VALUE",
status_code = 503L
)
remote_failure <- with_failure_metadata(
remote_failure,
failure_metadata("transient", retryable = TRUE, retry_after_seconds = 15)
)
stopifnot(isTRUE(is_retryable(remote_failure)))
condition_failure(remote_failure)$retry_after_seconds
#> [1] 15condition_to_json() serialises conditions in the
language-neutral DataExcept format. URL credentials are redacted before
export.
payload <- condition_to_json(remote_failure, pretty = TRUE)
stopifnot(is_envelope(payload))
stopifnot(!grepl("SECRET_VALUE", payload, fixed = TRUE))
cat(payload)
#> {
#> "type": "ApiError",
#> "module": "dataexcept",
#> "message": "API call failed: https://api.example.com/***?api_key=*** (status 503)",
#> "failure": {
#> "kind": "transient",
#> "retryable": true,
#> "retry_after_seconds": 15
#> },
#> "attributes": {
#> "endpoint": "https://api.example.com/***?api_key=***",
#> "status_code": 503
#> }
#> }The same payload can be read back into an R condition.
Python reference envelopes ship with the package, so this example does not require a Python installation.
fixture <- system.file(
"schema", "fixtures", "ordinary-exception.json",
package = "dataexcept"
)
stopifnot(nzchar(fixture))
python_payload <- paste(readLines(fixture, warn = FALSE), collapse = "\n")
python_error <- envelope_to_condition(python_payload)
stopifnot(inherits(python_error, "dataexcept_validation_error"))
condition_type(python_error)
#> [1] "ValidationError"
condition_failure(python_error)$kind
#> [1] "permanent"For multiple failures, use one envelope per line of JSON. Consumers
can inspect type and failure without knowing R
condition classes.
An observability event combines the envelope with a stable operation name and execution identifiers. Avoid using run identifiers as metric dimensions.
context <- operation_context(
system = "batch",
component = "orders",
operation = "validate_orders",
job_id = "job-42"
)
cat(condition_to_event_json(caught, operation_context = context))
#> {"event":"exception","exception":{"type":"MissingColumnError","module":"dataexcept","message":"Missing required column 'customer_id' in data frame 'orders'","failure":{"kind":"unknown","retryable":null,"retry_after_seconds":null},"attributes":{"column":"customer_id","dataframe":"orders"}},"operation":{"system":"batch","component":"orders","operation":"validate_orders","job_id":"job-42"}}The online documentation covers grouped errors, classed warnings, Pino, OpenTelemetry and the shared protocol.