Validation - Rethrow errors with more information
assert_from_msg.RdPackages like this one, checkmate, and chk provide functions to check
objects and return a message or abort if the check fails. This function
allows the user to catch that message/error and generate a condition with
higher flexibility than the original package.
Arguments
- fun
[
\(){}]For
assert_from_msg(): a function that checks an object and returnsTRUEif the check passes, or a message (character(1)) if it fails.For
assert_from_error(): a function that checks an object and raises an error if the check fails.
- ...
[
anyeach] Objects to check.- args_fun
[
list()] Additional arguments to pass tofun.- x_names
[
character()|NULL] The names of...to print in messages. IfNULL, the name is inferred from...'s expressions.- env
[
environment()|call()|NULL|missing_arg()] The call to inform as the origin of the error, passed torlang::abort():An environment in the call stack or a hard-coded defused call.
NULLfor no information.missing_arg()to use the assert function itself.The default is
caller_env(), to display the function where the assertion was called.
- args_cnd
[
list()] Additional arguments passed tocli::cli_abort().
Examples
# Using checkmate::check_* functions:
# try({
# f <- \(x) test_msg(checkmate::check_numeric, x, any.missing = FALSE)
# f(c(1, NA))
# })
#> Error in `f()`:
#> ! Argument `x` contains missing values (element 2)
# Multiple arguments with the same test:
# try({
# g <- \(x, y) test_msgs(checkmate::check_logical, x, y, args = list(len = 1))
# g(TRUE, c(FALSE, TRUE))
# })
#> Error in `g()`:
#> ! Argument `y` must have length 1, but has length 2