static T capture()

in lib/src/chain.dart [77:114]


  static T capture<T>(T Function() callback,
      {void Function(Object error, Chain)? onError,
      bool when = true,
      bool errorZone = true,
      Map<Object?, Object?>? zoneValues}) {
    if (!errorZone && onError != null) {
      throw ArgumentError.value(
          onError, 'onError', 'must be null if errorZone is false');
    }

    if (!when) {
      if (onError == null) return runZoned(callback, zoneValues: zoneValues);
      return runZonedGuarded(callback, (error, stackTrace) {
        onError(error, Chain.forTrace(stackTrace));
      }, zoneValues: zoneValues) as T;
    }

    var spec = StackZoneSpecification(onError, errorZone: errorZone);
    return runZoned(() {
      try {
        return callback();
      } on Object catch (error, stackTrace) {
        // Forward synchronous errors through the async error path to match the
        // behavior of `runZonedGuarded`.
        Zone.current.handleUncaughtError(error, stackTrace);

        // If the expected return type of capture() is not nullable, this will
        // throw a cast exception. But the only other alternative is to throw
        // some other exception. Casting null to T at least lets existing uses
        // where T is a nullable type continue to work.
        return null as T;
      }
    }, zoneSpecification: spec.toSpec(), zoneValues: {
      ...?zoneValues,
      _specKey: spec,
      StackZoneSpecification.disableKey: false
    });
  }