Title: | Result Type for Safely Handling Operations that can Succeed or Fail |
---|---|
Description: | Allows wrapping values in success() and failure() types to capture the result of operations, along with any status codes. Risky expressions can be wrapped in as_result() and functions wrapped in result() to catch errors and assign the relevant result types. Monadic functions can be bound together as pipelines or transaction scripts using then_try(), to gracefully handle errors at any step. |
Authors: | Soumya Ray [aut, cre, cph] |
Maintainer: | Soumya Ray <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2024-11-16 03:03:02 UTC |
Source: | https://github.com/soumyaray/result |
result
type, choosing between success
and failure
based on the outcome of the expression.Use as_result
on expressions whose outcomes are not known in advance or not
safe to be examined. The expression will be evualted immediately and wrapped
in success
if it produces a value or failure
if it produces an
error. If the expression produces a warning, it will be wrapped in
success
or failure
depending on the fail_on_warning
argument.
as_result(.expr, detect_warning = TRUE, fail_on_warning = TRUE)
as_result(.expr, detect_warning = TRUE, fail_on_warning = TRUE)
.expr |
expression to evaluate |
detect_warning |
logical, whether to detect warnings; note
|
fail_on_warning |
logical, whether to treat warnings as |
result
object of subclass success
or failure
as_result(42) as_result(1 + 1) stopper <- as_result(stop("This is my error message")) is_failure(stopper) value(stopper) as_result(warning("You've been warned")) |> is_success() as_result(warning("You've been warned"), fail_on_warning = FALSE) |> value()
as_result(42) as_result(1 + 1) stopper <- as_result(stop("This is my error message")) is_failure(stopper) value(stopper) as_result(warning("You've been warned")) |> is_success() as_result(warning("You've been warned"), fail_on_warning = FALSE) |> value()
If the second object is a function, its return value will be wrapped in a
result
object of subclass success
or failure
depending
on whether the function produces an error or warning. Bind is aliased with
then_try
.
If the second object is a function, its return value will be wrapped in a
result
object of subclass success
or failure
depending
on whether the function produces an error or warning. then_try
is
aliased with bind
.
bind(last_result, next_obj, ...) then_try(last_result, next_obj, ...)
bind(last_result, next_obj, ...) then_try(last_result, next_obj, ...)
last_result |
|
next_obj |
|
... |
additional arguments to pass to |
result
object of subclass success
or failure
result
object of subclass success
or failure
times3 <- function(x, succeeds = TRUE) { if (succeeds) success(x * 3) else failure("func1 failed") } success(5) |> bind(times3) |> value() success(5) |> bind(times3, succeeds = FALSE) |> is_failure() failure("failed from the start") |> bind(times3) |> is_failure() failure("failed from the start") |> bind(times3) |> value() times3 <- function(x, succeeds = TRUE) { if (succeeds) success(x * 3) else failure("func1 failed") } success(5) |> then_try(times3) |> value() success(5) |> then_try(times3, succeeds = FALSE) |> is_failure() failure("failed from the start") |> then_try(times3) |> is_failure() failure("failed from the start") |> then_try(times3) |> value()
times3 <- function(x, succeeds = TRUE) { if (succeeds) success(x * 3) else failure("func1 failed") } success(5) |> bind(times3) |> value() success(5) |> bind(times3, succeeds = FALSE) |> is_failure() failure("failed from the start") |> bind(times3) |> is_failure() failure("failed from the start") |> bind(times3) |> value() times3 <- function(x, succeeds = TRUE) { if (succeeds) success(x * 3) else failure("func1 failed") } success(5) |> then_try(times3) |> value() success(5) |> then_try(times3, succeeds = FALSE) |> is_failure() failure("failed from the start") |> then_try(times3) |> is_failure() failure("failed from the start") |> then_try(times3) |> value()
failure
type of result
Wraps a value in failure
type of result
failure(value = "failed", status = "error")
failure(value = "failed", status = "error")
value |
any object to wrap |
status |
character string of the result (typically short) |
result
object of subclass failure
failure() failure(42)
failure() failure(42)
failure
classChecks if an object is of failure
class
is_failure(obj)
is_failure(obj)
obj |
object to check |
TRUE
if object is of failure
class,
FALSE
otherwise
is_failure(success()) is_failure(failure())
is_failure(success()) is_failure(failure())
result
classChecks if an object is of result
class
is_result(obj)
is_result(obj)
obj |
object to check |
TRUE
if object is of result
class,
FALSE
otherwise
is_result(success()) is_result(failure()) is_result(42)
is_result(success()) is_result(failure()) is_result(42)
success
classChecks if an object is of success
class
is_success(obj)
is_success(obj)
obj |
object to check |
TRUE
if object is of success
class,
FALSE
otherwise
is_success(success()) is_success(failure())
is_success(success()) is_success(failure())
result
monad for later evaluation.Use result
on functions whose outcomes are not known in advance or not
safe to be examined. The function will not be evaluated until the monad is
explicitly called.
result(.fn, detect_warning = TRUE, fail_on_warning = TRUE)
result(.fn, detect_warning = TRUE, fail_on_warning = TRUE)
.fn |
function to wrap |
detect_warning |
logical, whether to detect warnings; note |
fail_on_warning |
logical, whether to treat warnings as |
function that returns a result
object of subclass
success
or failure
crashy <- function() stop("Go no further") safely_call_crashy <- result(crashy) safely_call_crashy() |> is_failure() calculate <- function(x, y) x + y safely_calculate <- result(calculate) safely_calculate(1, 2) |> value()
crashy <- function() stop("Go no further") safely_call_crashy <- result(crashy) safely_call_crashy() |> is_failure() calculate <- function(x, y) x + y safely_calculate <- result(calculate) safely_calculate(1, 2) |> value()
result
Extracts the status of a result
status(obj)
status(obj)
obj |
|
status of the result
status(success("datafile.md", status = "created"))
status(success("datafile.md", status = "created"))
success
type of result
success
is a constructor function for result
class.
success(value = "done", status = "ok")
success(value = "done", status = "ok")
value |
any object to wrap |
status |
character string of the result (typically short) |
result
object of subclass success
success() success(42)
success() success(42)
result
Extracts the value of a result
value(obj)
value(obj)
obj |
|
value object wrapped by result
value(success(42))
value(success(42))