guardCallback method

VoidCallback guardCallback(
  1. VoidCallback action, {
  2. String? source,
  3. FutureOr<void> onError(
    1. BaseException e
    )?,
})

Wraps a synchronous callback, rethrowing the normalized error to its caller.

Reports the error automatically and updates the fallback in a microtask.

Implementation

VoidCallback guardCallback(
  VoidCallback action, {
  String? source,
  FutureOr<void> Function(BaseException e)? onError,
}) {
  return () {
    try {
      action();
    } catch (e, s) {
      final normalized = _normalize(e, s, source: source);
      scheduleMicrotask(() => _captureAutomatic(normalized, onError));
      Error.throwWithStackTrace(normalized, s);
    }
  };
}