Report several failures as one¶
Source: R/groups.R ยท Help: ?condition_group
condition_group() combines several conditions into one error, so that a
check which finds many problems can report all of them instead of stopping
at the first. It is written to an envelope as an exception group -- the
exceptions array the schema defines, which Python's ExceptionGroup also
produces -- with every member rendered in full, chains included.
Usage¶
condition_group(
conditions,
message = NULL,
...,
class = character(),
parent = NULL,
call = NULL,
failure = NULL
)
group_members(cnd)
collect_errors(...)
Arguments¶
conditions- A non-empty list of conditions.
message- A short description of what failed, or
NULL. The count of failures is appended:"orders failed validation (3 failures)". ...- Named fields to store on the group, as in
new_dataexcept_error(). class- Additional R classes, most specific first.
parent- The condition that caused the whole group, or
NULL. call- The call to report, or
NULL. failure- Failure metadata from
failure_metadata(), orNULLto derive it from the members. cnd- A condition.
Value¶
condition_group() returns a condition of class
dataexcept_condition_group. collect_errors() returns a list of the
errors caught, named after the arguments that signalled them when those
are named, and empty when there were none. group_members() returns a
list of conditions, empty for a condition that is not a group.
Details¶
collect_errors() is the way to gather the members: it evaluates each of
its arguments in turn, catches any error one of them signals, and carries on
with the next. group_members() returns the members of a group, whether it
was built here or read from an envelope.
A group is caught by a dataexcept_condition_group handler, and by
dataexcept_error like every other dataexcept error. A group read from an
envelope -- including a Python ExceptionGroup -- has the
dataexcept_condition_group class too.
The group's failure metadata, unless given, is its members' when they all agree, and unclassified otherwise: a batch of permanent validation failures is permanent, but a mix of transient and permanent failures has no single answer to whether a retry would help. When the members agree and some give a retry delay, the longest one is used.
Examples¶
orders <- data.frame(id = 1:3, amount = c("10", "20", "30"))
require_column <- function(df, column) {
if (!column %in% names(df)) stop(missing_column_error(column, dataframe = "orders"))
}
require_numeric <- function(df, column) {
if (!is.numeric(df[[column]])) {
stop(dtype_mismatch_error(column, "numeric", class(df[[column]])[1]))
}
}
errors <- collect_errors(
require_column(orders, "customer_id"),
require_column(orders, "amount"),
require_numeric(orders, "amount")
)
length(errors)
err <- tryCatch(
if (length(errors) > 0) stop(condition_group(errors, "orders failed validation")),
dataexcept_condition_group = identity
)
conditionMessage(err)
vapply(group_members(err), function(e) class(e)[1], character(1))
cat(condition_to_json(err, pretty = TRUE))