Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cli to give detailed, friendly error messages #206

Open
capnrefsmmat opened this issue Nov 4, 2023 · 0 comments
Open

Use cli to give detailed, friendly error messages #206

capnrefsmmat opened this issue Nov 4, 2023 · 0 comments

Comments

@capnrefsmmat
Copy link
Contributor

There are a few functions that call stop() to signal invalid inputs or other user errors. We should use cli_abort() for these. It allows you to give detail on the types of arguments and what was expected, in a much friendlier way than just pasting together a message in stop().

Here are a couple examples from a package of mine:

  if (!inherits(population, "population")) {
    cli_abort(c("{.arg population} argument must be a population defined with {.fn population}",
                "i" = "Received {.type {population}} instead"))
  }
    cli_abort(
      c("Diagnostic function {.arg fn} must return a data frame or tibble",
        "x" = "{.arg fn} returned a result of class {.cls {class(x)}}"),
      class = "regressinator_diagnostic_class",
      call = caller_env()
    )

These get pretty-printed with colored formatting, and the markup like {.cls} produces nice formatting. For instance, here's what you get from the second example:

Error in `sampling_distribution()`:
! diagnostic function `fn` must return a data frame or tibble
✖ `fn` returned a result of class <character>
Run `rlang::last_trace()` to see where the error occurred.
@dsweber2 dsweber2 added this to the Z: epidatr & epidatpy milestone Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants