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(...)
#>   2. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. ├─base (local) tryCatchOne(...)
#>   4. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(...)
#>  13.   └─pkgdown::build_site(...)
#>  14.     └─pkgdown:::build_site_local(...)
#>  15.       └─pkgdown::build_reference(...)
#>  16. ─pkgdown:::unwrap_purrr_error(...)
#>  17. └─base::withCallingHandlers(...)
#>  18.         └─purrr::map(...)
#>  19.           └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  20. ─purrr:::with_indexed_errors(...)
#>  21. └─base::withCallingHandlers(...)
#>  22. purrr:::call_with_cleanup(...)
#>  23.             └─pkgdown (local) .f(.x[[i]], ...)
#>  24. base::withCallingHandlers(...)
#>  25.               └─pkgdown:::data_reference_topic(...)
#>  26.                 └─pkgdown:::run_examples(...)
#>  27.                   └─pkgdown:::highlight_examples(code, topic, env = env)
#>  28.                     └─downlit::evaluate_and_highlight(...)
#>  29.                       └─evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>  30. base::withRestarts(...)
#>  31. └─base (local) withRestartList(expr, restarts)
#>  32. ├─base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]])
#>  33. │ └─base (local) doWithOneRestart(return(expr), restart)
#>  34. └─base (local) withRestartList(expr, restarts[-nr])
#>  35. └─base (local) withOneRestart(expr, restarts[[1L]])
#>  36. └─base (local) doWithOneRestart(return(expr), restart)
#>  37. evaluate:::with_handlers(...)
#>  38. ├─base::eval(call)
#>  39. │ └─base::eval(call)
#>  40. └─base::withCallingHandlers(...)
#>  41. ─base::withVisible(eval(expr, envir))
#>  42.                         └─base::eval(expr, envir)
#>  43.                           └─base::eval(expr, envir)
catch_cnd(signal("my_condition", message = "a condition"))
#> <my_condition: a condition>