rlang icon indicating copy to clipboard operation
rlang copied to clipboard

Helper to wrap error with a hint

Open hadley opened this issue 1 year ago • 3 comments

In some situations you know the probable cause of an error, and there's a specific fix that the user might want to try first. Ideally this would be added as some sort of trailing hint, but we don't have a great technique for that currently.

e.g. https://github.com/tidyverse/dbplyr/pull/1459, https://github.com/r-dbi/odbc/pull/744/files#diff-ef8dcd24d5337937983c4c56ba9146ec1fd06d22851849ff570ffdc7ad051a58R33

hadley avatar Feb 21 '24 14:02 hadley

Does this help?

wrap <- function(expr) {
  withCallingHandlers(
    expr,
    error = function(cnd) {
      abort(cnd_message(cnd), footer = c("i" = "bar"))
    }
  )
}

wrap(1 + "")
#> Error in `h()`:
#> ! non-numeric argument to binary operator
#> ℹ bar

lionel- avatar May 31 '24 11:05 lionel-

Yes!

hadley avatar May 31 '24 12:05 hadley

oh weird :/

wrap(cli::cli_abort(c("a", i = "b", x = "c")))
#> Error:
#> ! a ℹ b ✖ c
#> ℹ bar

lionel- avatar May 31 '24 12:05 lionel-