bar <- function(x) {
foo(x)
paste("input:", x)
}
foo <- function(x, arg = caller_arg(x), call = caller_env()) {
if (is.character(x)) {
return(invisible(NULL))
}
msg <- paste0("`", arg, "` must be a character vector.")
abort(msg, call = call)
}
If I execute bar()
, I get a proper error message with the right function call:
bar(1)
#> Error in `bar()`:
#> ! `x` must be a character vector.
#> Run `rlang::last_error()` to see where the error occurred.
Now if I add more nesting, I won't get a proper error message (which points to bar()
instead of blah()
) unless I add a call
parameter to bar
/blah
, call foo()
in blah()
or change the number of callers to go back.
blah <- function(x) {
bar(x)
}
blah(1)
#> Error in `bar()`:
#> ! `x` must be a character vector.
#> Run `rlang::last_error()` to see where the error occurred.
I can propagate the user function environment through the function tree by using caller_env(sys.parent)
in foo()
. I'm just wondering if there's a rlang
function for that already.