Simplified Error Handling

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.


slap

Lifecycle:experimental CRANstatus R-CMD-check

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" 

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("slap")

2024.4.1 by Romain François, 2 years ago


https://github.com/tadascience/slap, https://slap.tada.science


Report a bug at https://github.com/tadascience/slap/issues


Browse source code at https://github.com/cran/slap


Authors: Romain François [aut, cre] , tada.science [cph, fnd]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, rlang

Suggests testthat


See at CRAN