Skip to content

Commit

Permalink
handle when message is a function
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 6, 2024
1 parent 76dc38b commit 627a9d2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by roxygen2: do not edit by hand

export("%!%")
export(slap)
import(cli)
import(rlang)
16 changes: 6 additions & 10 deletions R/slap.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#' @name slap
#' @export
`%!%` <- function(expr, message) {
message <- enexpr(message)

quo <- quo(
withCallingHandlers(
{{ expr }},
error = function(.__slap__.) {
message <- !!message
error = function(err) {
message <- {{ message }}
if (is.function(message)) {
message <- message(err)
}

error_call <- env$.__error_call__.
if (is.null(error_call)) {
Expand All @@ -40,16 +41,11 @@
error_call <- NULL
}

cli::cli_abort(message, parent = .__slap__., call = error_call)
cli::cli_abort(message, parent = err, call = error_call)
}
)
)

env <- caller_env()
eval_tidy(quo, env = env, data = list(env = env))
}

#' @export
slap <- function(error_call = current_env()) {
get(".__slap__.", caller_env()) %||% cli_abort("{.fn slap()} can only be used on the rhs of a `%!%`.", call = error_call)
}

0 comments on commit 627a9d2

Please sign in to comment.