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. pkgdown::deploy_to_branch(new_process = FALSE) #> 2. pkgdown::build_site(...) #> 3. pkgdown:::build_site_local(...) #> 4. pkgdown::build_reference(...) #> 5. purrr::map(...) #> 6. pkgdown .f(.x[[i]], ...) #> 8. pkgdown:::data_reference_topic(...) #> 9. pkgdown:::run_examples(...) #> 10. pkgdown:::highlight_examples(code, topic, env = env) #> 11. downlit::evaluate_and_highlight(...) #> 12. evaluate::evaluate(code, child_env(env), new_device = TRUE) #> 13. evaluate:::evaluate_call(...) #> 23. [ base::eval(...) ] with 1 more call #> 25. rlang::catch_cnd(abort("an error")) #> 31. base::force(expr)
catch_cnd(signal("my_condition", message = "a condition"))
#> <my_condition: a condition>