Skip to contents

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.

Usage

result(fn, ...)

Arguments

fn

A function to wrap.

...

Additional optional arguments that may be used by individual methods.

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"