Skip to content

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.

Usage

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::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.                         └─evaluate:::evaluate_call(...)
#>  20. ─evaluate (local) timing_fn(...)
#>  21. ─evaluate (local) handle(...)
#>  22. └─base::try(f, silent = TRUE)
#>  23. └─base::tryCatch(...)
#>  24. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  25. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  26. └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  27. ─base::withCallingHandlers(...)
#>  28. ─base::withVisible(...)
#>  29.                           └─evaluate:::eval_with_user_handlers(expr, envir, enclos, user_handlers)
#>  30.                             └─base::eval(expr, envir, enclos)
#>  31.                               └─base::eval(expr, envir, enclos)
catch_cnd(signal("my_condition", message = "a condition"))
#> <my_condition: a condition>