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> #> an error #> Backtrace: #> 1. base::tryCatch(...) #> 13. pkgdown::build_site(...) #> 14. pkgdown:::build_site_local(...) #> 15. pkgdown::build_reference(...) #> 16. purrr::map(...) #> 17. pkgdown:::.f(.x[[i]], ...) #> 18. pkgdown:::data_reference_topic(...) #> 20. pkgdown:::as_data.tag_examples(...) #> 21. purrr::pmap_chr(...) #> 22. pkgdown:::.f(...) #> 25. evaluate::evaluate(code, env, new_device = TRUE) #> 26. evaluate:::evaluate_call(...) #> 36. [ base::eval(...) ] with 1 more call #> 38. rlang::catch_cnd(abort("an error")) #> 44. base::force(expr) /home/travis/build/r-lib/rlang/R/cnd.R:539:6 #> Call `rlang::last_trace()` to see the full backtrace
catch_cnd(cnd_signal("my_condition", .msg = "a condition"))
#> <simpleError in cnd_signal("my_condition", .msg = "a condition"): unused argument (.msg = "a condition")>