guardCallback method

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

Wrap a synchronous callback (e.g., onTap) to capture thrown errors.

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(() async {
        await onError?.call(normalized);
        _setError(normalized);
        await _report(normalized);
      });
      // Re-throw so calling code can handle if it wants.
      // In typical UI callbacks, this is fine and will still show fallback.
      // ignore: only_throw_errors
      throw normalized;
    }
  };
}