Description
Simplified Error Handling.
Description
Alternative to using withCallingHandlers() in the simple case of catch and rethrow. The `%!%` operator evaluates the expression on its left hand side, and if an error occurs, the right hand side is used to construct a new error that embeds the original error.
README.md
slap
The goal of slap is simplify error handling.
Installation
pak::pak("tadascience/slap")
Example
library(dplyr)
library(slap)
# suppose you have a function that throws an error
boom <- function() stop("An error occured in boom()")
# and you want to use it in e.g. dplyr::summarise()
# summarise(mtcars, mpg = boom())
# if you want to catch it and rethrow an error that is more
# meaningful to you, one way is to use withCallingHandlers()
withCallingHandlers(
summarise(mtcars, mpg = boom()),
error = function(err) {
cli::cli_abort("ouch", parent = err)
}
)
# but that's kind of boring, so instead you can use the
# slap operator %!% to slap away the eror
summarise(mtcars, mpg = boom()) %!% "ouch"
# or the double slap operator %!!% if you don't want to keep the parent error
summarise(mtcars, mpg = boom()) %!!% "ouch"