runCatching<R> method
FutureOr<R?>
runCatching<R>(
- FutureOr<
R?> block(), { - FutureOr<
R?> onSuccess(- R data
- FutureOr<
R?> onFailure(- Object e,
- StackTrace s
- dynamic ignoreSkipError = true,
ignoreSkipError same as update((o)=>null)
true: SkipError will not trigger onFailure when true
ref skpIf/skpNull
Implementation
@visibleForTesting
@protected
FutureOr<R?> runCatching<R>(
FutureOr<R?> Function() block, {
FutureOr<R?> Function(R data)? onSuccess,
FutureOr<R?> Function(Object e, StackTrace s)? onFailure,
ignoreSkipError = true,
}) {
FutureOr<R?> onCatchError(e, s) {
return (e is SkipError && ignoreSkipError) ? null : onFailure?.call(e, s);
}
try {
final rst = block();
if (rst == null) return null;
if (rst is R) return (onSuccess ?? (r) => r).call(rst);
if (rst is Future<R>) {
return rst.then(
(e) => (onSuccess ?? (r) => r).call(e),
onError: onCatchError,
);
} else if (rst is Future<R?>) {
return rst.then(
(e) => e == null ? null : onSuccess?.call(e),
onError: onCatchError,
);
}
throw SkipError(
'Unknown block result type [${rst.runtimeType}]; result [$rst];\n'
'Please create new issues (https://github.com/Hu-Wentao/flowr/issues/new)',
);
} catch (e, s) {
return onCatchError(e, s);
}
}