rlang icon indicating copy to clipboard operation
rlang copied to clipboard

`error_arg()` and `error_call()` should have better fall backs when called directly

Open hadley opened this issue 7 months ago • 3 comments

library(rlang)

foo <- function(x, arg = caller_arg(x), call = caller_env()) {
  cli::cli_abort("Error {.arg {x}}", call = call)
}
foo(123)
#> Error:
#> ! Error `123`

Created on 2023-11-09 with reprex v2.0.2.9000

Ideally this would be

#> Error: foo()
#> ! Error `x`

hadley avatar Nov 09 '23 14:11 hadley

Unfortunately I don't think we can do better here.

lionel- avatar Nov 13 '23 07:11 lionel-

Though we could detect calls at top-level. The behaviour would then differ when called directly vs in a browser or in testthat or markdown.

lionel- avatar Nov 13 '23 08:11 lionel-

The thing that makes me think we could do better is that it's the same appearance at the top level and in snapshot:

Code
  foo(123)
Condition
  Error:
  ! Error `123`

hadley avatar Nov 13 '23 13:11 hadley