Skip to content

Commit

Permalink
+ slap()
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 6, 2024
1 parent 5e1d7df commit 76dc38b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

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

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

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

cli::cli_abort(message, parent = err, call = error_call)
cli::cli_abort(message, parent = .__slap__., 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 76dc38b

Please sign in to comment.