r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
489 stars 131 forks source link

Helper to wrap error with a hint #1692

Open hadley opened 4 months ago

hadley commented 4 months ago

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

lionel- commented 1 month ago

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
hadley commented 1 month ago

Yes!

lionel- commented 1 month ago

oh weird :/

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