This is a small wrapper around tryCatch() that captures any condition signalled while evaluating its argument. It is useful for situations where you expect a specific condition to be signalled, for debugging, and for unit testing.

catch_cnd(expr, classes = "condition")

Arguments

expr

Expression to be evaluated with a catching condition handler.

classes

A character vector of condition classes to catch. By default, catches all conditions.

Value

A condition if any was signalled, NULL otherwise.

Examples

catch_cnd(10)
#> NULL
catch_cnd(abort("an error"))
#> <error/rlang_error>
#> Error:
#> ! an error
#> ---
#> Backtrace:
#>   1. base::tryCatch(...)
#>  12. global `<fn>`(...)
#>  13. pkgdown::build_site(...)
#>  14. pkgdown:::build_site_local(...)
#>        at pkgdown/R/build.R:359:4
#>  15. pkgdown::build_reference(...)
#>        at pkgdown/R/build.R:432:2
#>  16. purrr::map(...)
#>        at pkgdown/R/build-reference.R:191:2
#>  17. pkgdown .f(.x[[i]], ...)
#>        at purrr/R/map.R:111:2
#>  19. pkgdown:::data_reference_topic(...)
#>        at pkgdown/R/build-reference.R:279:2
#>  20. pkgdown:::run_examples(...)
#>        at pkgdown/R/build-reference.R:363:4
#>  21. pkgdown:::highlight_examples(code, topic, env = env)
#>        at pkgdown/R/rd-example.R:37:4
#>  22. downlit::evaluate_and_highlight(...)
#>        at pkgdown/R/highlight.R:32:2
#>  23. evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>        at downlit/R/evaluate.R:50:2
#>  24. evaluate:::evaluate_call(...)
#>        at evaluate/R/eval.r:73:4
#>  34. evaluate:::eval_with_user_handlers(expr, envir, enclos, user_handlers)
#>        at evaluate/R/eval.r:198:4
#>  35. [ base::eval(...) ] with 1 more call
#>        at evaluate/R/eval.r:234:4
catch_cnd(signal("my_condition", message = "a condition"))
#> <my_condition: a condition>