guardFuture<T> method

Future<T> guardFuture<T>(
  1. Future<T> action(), {
  2. String? source,
  3. FutureOr<void> onSuccess(
    1. T value
    )?,
  4. FutureOr<void> onError(
    1. BaseException e
    )?,
})

Guard an async operation and route failures into this boundary.

Implementation

Future<T> guardFuture<T>(
  Future<T> Function() action, {
  String? source,
  FutureOr<void> Function(T value)? onSuccess,
  FutureOr<void> Function(BaseException e)? onError,
}) async {
  final res = await guard<T>(
    action,
    source: source,
    onSuccess: onSuccess,
    onError: (e) async {
      await onError?.call(e);
      _setError(e);
    },
  );

  return res.match(
    ok: (v) => v,
    err: (e) => throw e, // propagate, while boundary shows fallback
  );
}