Skip to content

Commit

Permalink
more documentation for %!%
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 23, 2024
1 parent 50f1fa2 commit 5d4f0e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
34 changes: 26 additions & 8 deletions R/slap.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
#' Slap Operator
#'
#' @param expr An expression or quosure to evaluate carefully
#' @param message A message meant to be formatted by [cli::cli_bullets()]
#' @param message A message meant to be formatted by [cli::cli_bullets()] or a
#' function.
#'
#' @return If `expr` succeeds, its result is returned.
#'
#' When `expr` generates an error, the `%!%` and `%!!%` operators
#' catch it and embed it in a new error thrown by [cli::cli_abort()]
#' with `message`.
#' catch it and embed it in a new error thrown by [cli::cli_abort()].
#'
#' If `message` evaluates to a character vector, it is used as the
#' `message` argument of [cli::cli_abort()].
#'
#' If `message` evaluates to a function, the function is called with one
#' argument: the caught error from evaluating `expr`.
#'
#' When the current environment has an `error_call` object, it is
#' used as the `call` argument of [cli::cli_abort()]
#' used as the `call` argument of [cli::cli_abort()].
#'
#' @examples
#'
#' # g() throws an error
#' g <- function() {
#' stop("ouch")
#' }
#'
#' # h() catches that error and embed it in a new error
#' # with "bam" as its message, the g() error as the parent error,
#' # and the caller environment as call=
#' h <- function(error_call = rlang::caller_env()) {
#' g() %!% "bam"
#' }
#'
#' # f() will be used as the error call
#' f <- function() {
#' h()
#' }
#'
#' \dontrun{
#' f()
#' }
#' # Error in `f()`:
#' # ! bam
#' # Caused by error in `g()`:
#' # ! ouch
#' tryCatch(f(), error = function(err) {
#' print(err, backtrace = FALSE)
#' })
#'
#' @name slap
#' @export
Expand Down
35 changes: 27 additions & 8 deletions man/slap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d4f0e0

Please sign in to comment.