Wrap an existing function to return Result
objects
result.Rd
Wrap up an existing function so that it returns values wrapped in a Success
if it finished computing and returns a value, and returns a Failure
if a
warning or error condition is intercepted during the functions execution.
Examples
my_awesome_function <- function(do_the_throw = FALSE) {
if(do_the_throw) {
stop("Oh no!")
} else {
return(100)
}
}
safe_awesome <- result(my_awesome_function)
safe_awesome()
#> <resultr::Success>
#> @ value: num 100
safe_awesome(do_the_throw = TRUE)
#> <resultr::Failure>
#> @ error:List of 2
#> .. $ message: chr "Oh no!"
#> .. $ call : language fn(...)
#> .. - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"