Skip to contents

When you use pkg_abort() to signal errors, you can use this function to test that those errors are generated as expected.

Usage

expect_pkg_error_classes(object, package, ...)

Arguments

object

An expression that is expected to throw an error.

package

(length-1 character) The name of the package to use in classes.

...

(character) Components of the class name, from least-specific to most.

Value

The classes of the error invisibly on success or the error on failure. Unlike most testthat expectations, this expectation cannot be usefully chained.

Examples

expect_pkg_error_classes(
  pkg_abort("stbl", "This is a test error", "test_subclass"),
  "stbl",
  "test_subclass"
)
#> <error/stbl-error-test_subclass>
#> Error:
#> ! This is a test error
#> ---
#> Backtrace:
#>      
#>   1. └─pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
#>   2.   └─pkgdown::build_site(...)
#>   3.     └─pkgdown:::build_site_local(...)
#>   4.       └─pkgdown::build_reference(...)
#>   5. ─pkgdown:::unwrap_purrr_error(...)
#>   6. └─base::withCallingHandlers(...)
#>   7.         └─purrr::map(...)
#>   8.           └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>   9. ─purrr:::with_indexed_errors(...)
#>  10. └─base::withCallingHandlers(...)
#>  11. purrr:::call_with_cleanup(...)
#>  12.             └─pkgdown (local) .f(.x[[i]], ...)
#>  13. base::withCallingHandlers(...)
#>  14.               └─pkgdown:::data_reference_topic(...)
#>  15.                 └─pkgdown:::run_examples(...)
#>  16.                   └─pkgdown:::highlight_examples(code, topic, env = env)
#>  17.                     └─downlit::evaluate_and_highlight(...)
#>  18.                       └─evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>  19. base::withRestarts(...)
#>  20. └─base (local) withRestartList(expr, restarts)
#>  21. ├─base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]])
#>  22. │ └─base (local) doWithOneRestart(return(expr), restart)
#>  23. └─base (local) withRestartList(expr, restarts[-nr])
#>  24. └─base (local) withOneRestart(expr, restarts[[1L]])
#>  25. └─base (local) doWithOneRestart(return(expr), restart)
#>  26. evaluate:::with_handlers(...)
#>  27. ├─base::eval(call)
#>  28. │ └─base::eval(call)
#>  29. └─base::withCallingHandlers(...)
#>  30. ─base::withVisible(eval(expr, envir))
#>  31.                         └─base::eval(expr, envir)
#>  32.                           └─base::eval(expr, envir)
try(
  expect_pkg_error_classes(
    pkg_abort("stbl", "This is a test error", "test_subclass"),
    "stbl",
    "different_subclass"
  )
)
#> Error : Expected `e` to have class "stbl-error-different_subclass"/"stbl-error"/"stbl-condition"/"rlang_error"/"error"/"condition".
#> Actual class: "stbl-error-test_subclass"/"stbl-error"/"stbl-condition"/"rlang_error"/"error"/"condition".