guard<T> method

Future<T> guard<T>(
  1. Future<T> action(), {
  2. String category = 'app.zone',
  3. bool fatal = true,
})

Implementation

Future<T> guard<T>(
  Future<T> Function() action, {
  String category = 'app.zone',
  bool fatal = true,
}) {
  final completer = Completer<T>();
  runZonedGuarded(
    () async {
      final result = await action();
      if (!completer.isCompleted) {
        completer.complete(result);
      }
    },
    (error, stackTrace) {
      unawaited(
        report(
          error,
          stackTrace,
          category: category,
          fatal: fatal,
        ),
      );
      if (!completer.isCompleted) {
        completer.completeError(error, stackTrace);
      }
    },
  );
  return completer.future;
}