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());
}
}expectreturns contained value ifOk, throws an exception with provided message ifErr.unwrapreturns contained value ifOk, throws an exception ifErr.unwrapOrreturns contained value ifOk, returns the provided fallback value ifErr.unwrapOrElsereturns contained value ifOk, returns the result of function provided ifErr(function takes in the contained Error type and returns a value type).expectErrreturns contained Error if Err, throws an exception with provided message ifOk.unwrapErrreturns contained Error ifErr, throws an exception ifOk.
inspectCalls the provided closure with the contained value (ifOk) without consuming the resultinspectErrCalls the provided closure with the contained Error (ifErr) without consuming the result.
isOkReturns true if ofOkvariant, false if not.isOkAndReturns true if the result isOkand the contained value matches the provided predicate function, otherwise returns false.isErrReturns true if ofErrvariant, false if not.isErrAndReturns true if the result isErrand the contained value matches the provided predicate function, otherwise returns false.
Okconverts aResult<T, E>to aT?, i.e, ifOk, T, ifErr, null.
whenConverts aResult<T, E>to aUgiven aU ErrMap(E)and aU okMap(T)mapOrElseConverts aResult<T, E>to aUgiven aU ErrMap(E)and aU okMap(T)mapOrConverts aResult<T, E>to aU, given aU fallbackandU okMap(T)mapConverts aResult<T, E>toResult<U, E>by applying the provided function if to contained value if ok, or returning the original Error if Err.mapErrConverts aResult<T, E>toResult<T, F>by applying the provided function if to contained Error if Err, or returning the original ok if ok.
containsReturns true if the result is anOkvalue containing the given value, otherwise returns falsecontainsErrReturns true if the result is anErrvalue 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 andor 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) |
collectConvert 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.