| 1 |
with_subscript_errors <- function(expr, type = "select") { |
| 2 |
withCallingHandlers( |
| 3 |
expr, |
| 4 |
vctrs_error_subscript = function(cnd) { |
| 5 |
cnd$subscript_action <- subscript_action(type) |
| 6 |
cnd$subscript_elt <- "column" |
| 7 |
cnd_signal(cnd) |
| 8 |
} |
| 9 |
) |
| 10 |
} |
| 11 |
|
| 12 |
with_chained_errors <- function(expr, action, call, eval_expr = NULL) { |
| 13 |
withCallingHandlers( |
| 14 |
expr, |
| 15 |
error = function(cnd) { |
| 16 |
eval_expr <- quo_squash(eval_expr) |
| 17 |
# Only display a message if there's useful context to add |
| 18 |
if (!is_call(eval_expr) || identical(cnd[["call"]], call2(eval_expr[[1]])) ) { |
| 19 |
msg <- "" |
| 20 |
} else { |
| 21 |
code <- as_label(eval_expr) |
| 22 |
msg <- cli::format_inline("In argument: {.code {code}}.") |
| 23 |
} |
| 24 |
cli::cli_abort(c("i" = msg), call = call, parent = cnd) |
| 25 |
} |
| 26 |
) |
| 27 |
} |
| 28 |
|
| 29 |
subscript_action <- function(type) { |
| 30 |
switch(validate_type(type), |
| 31 |
select = "select", |
| 32 |
rename = "rename", |
| 33 |
relocate = "relocate", |
| 34 |
pull = "extract" |
| 35 |
) |
| 36 |
} |
| 37 |
validate_type <- function(type) { |
| 38 |
# We might add `recode` in the future |
| 39 |
if (!is_string(type, c("select", "rename", "relocate", "pull"))) { |
| 40 |
cli::cli_abort("Unexpected value for {.arg tidyselect_type}.", .internal = TRUE) |
| 41 |
} |
| 42 |
type |
| 43 |
} |