as_result {result}R Documentation

Wraps an expression in result type, choosing between success and failure based on the outcome of the expression.

Description

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.

Usage

as_result(.expr, detect_warning = TRUE, fail_on_warning = TRUE)

Arguments

.expr

expression to evaluate

detect_warning

logical, whether to detect warnings; note as_result() cannot capture the outcome of an expression if it catches warnings, so use detect_warning = TRUE only if you want to capture the warning message (e.g., after a side-effect).

fail_on_warning

logical, whether to treat warnings as failure or success.

Value

result object of subclass success or failure

Examples

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()

[Package result version 0.1.0 Index]