Typed Error-handling for dart.
Result<T, E>
is a type used for returning and propagating Errors. It is a type with the variants, Ok(T)
, representing success and containing a value, and Err(E)
, representing Error and containing an Error value.
In the dependencies:
section of your pubspec.yaml
, add the following line:
dependencies:
okay: <latest_version>
import 'package:okay/okay.dart';
class FallibleOpFailure {}
final result = fallibleOp();
final goodString = switch(result) {
Ok(v: final value) => value,
Err(e: final error) => error,
};
// ------------- OR --------------
final goodString = result.when(
ok: (value) {
print('Success with value: $value');
return value;
},
err: (error) {
print('Failure with Error: $Error');
return 'Fallback string';
},
);
useString(goodString);
void useString(String value) {
//
}
Result<String, FallibleOpFailure> fallibleOp() {
if (true) {
return Ok('Very good string');
} else {
return Err(FallibleOpFailure());
}
}
expect
returns contained value ifOk
, throws an exception with provided message ifErr
.unwrap
returns contained value ifOk
, throws an exception ifErr
.unwrapOr
returns contained value ifOk
, returns the provided fallback value ifErr
.unwrapOrElse
returns contained value ifOk
, returns the result of function provided ifErr
(function takes in the contained Error type and returns a value type).expectErr
returns contained Error if Err, throws an exception with provided message ifOk
.unwrapErr
returns contained Error ifErr
, throws an exception ifOk
.
inspect
Calls the provided closure with the contained value (ifOk
) without consuming the resultinspectErr
Calls the provided closure with the contained Error (ifErr
) without consuming the result.
isOk
Returns true if ofOk
variant, false if not.isOkAnd
Returns true if the result isOk
and the contained value matches the provided predicate function, otherwise returns false.isErr
Returns true if ofErr
variant, false if not.isErrAnd
Returns true if the result isErr
and the contained value matches the provided predicate function, otherwise returns false.
Ok
converts aResult<T, E>
to aT?
, i.e, ifOk
, T, ifErr
, null.
when
Converts aResult<T, E>
to aU
given aU ErrMap(E)
and aU okMap(T)
mapOrElse
Converts aResult<T, E>
to aU
given aU ErrMap(E)
and aU okMap(T)
mapOr
Converts aResult<T, E>
to aU
, given aU fallback
andU okMap(T)
map
Converts aResult<T, E>
toResult<U, E>
by applying the provided function if to contained value if ok, or returning the original Error if Err.mapErr
Converts aResult<T, E>
toResult<T, F>
by applying the provided function if to contained Error if Err, or returning the original ok if ok.
contains
Returns true if the result is anOk
value containing the given value, otherwise returns falsecontainsErr
Returns true if the result is anErr
value containing the given value, otherwise returns false
These methods treat the Result
as a boolean value, where the Ok
variant is acts like true
and Err
acts like false
.
The and
and
or take another Result
as input, and produce Result
as output. The and
method can produce a Result<U, E>
value having a different inner type U
than Result<T, E>
. The or
method can produce a Result<T, F>
value having a different Error type F
than Result<T, E>
.
method | this | input | output |
---|---|---|---|
and ) |
Err(e) |
-- | Err(e) |
and ) |
Ok(x) |
Err(d) |
Err(d) |
and ) |
Ok(x) |
Ok(y) |
Ok(y) |
or |
Err(e) |
Err(d) |
Err(d) |
or |
Err(e) |
Ok(y) |
Ok(y) |
or |
Ok(x) |
-- | Ok(x) |
The andThen
and orElse
methods take a function as input, and only evaluate the function when they need to produce a new value. The andThen
method can produce a Result<U, E>
value having a different inner type U
than Result<T, E>
. The orElse
method can produce a Result<T, F>
value having a different Error type F
than Result<T, E>
.
method | this | function input | function result | output |
---|---|---|---|---|
andThen |
Err(e) |
-- | -- | Err(e) |
andThen |
Ok(x) |
x |
Err(d) |
Err(d) |
andThen |
Ok(x) |
x |
Ok(y) |
Ok(y) |
orElse |
Err(e) |
e |
Err(d) |
Err(d) |
orElse |
Err(e) |
e |
Ok(y) |
Ok(y) |
orElse |
Ok(x) |
-- | -- | Ok(x) |
collect
Convert anIterable<Result<T, E>>
to aResult<Iterable<T>, E>
. If there is an Err in the iterable, the first Err is returned.- collectOr Converts an
Iterable<Result<T, E>>
to a<Iterable<T>
. All Err values are replaced by the provided fallback. - collectOrElse Converts an
Iterable<Result<T, E>>
to a<Iterable<T>
. All Err values are replaced by the result of the provided function. - sieve Converts an
Iterable<Result<T, E>>
to a<Iterable<T>
. All Err values skipped. - sieveErr Converts an
Iterable<Result<T, E>>
to a<Iterable<E>
. All ok values skipped.