call method

  1. @override
FutureOr<void> call(
  1. Hub hub,
  2. SentryOptions options
)
override

A Callable method for the Integration interface

Implementation

@override
FutureOr<void> call(Hub hub, SentryOptions options) {
  runZonedGuarded(
    () async {
      await _runner();
    },
    (exception, stackTrace) async {
      // runZonedGuarded doesn't crash the App.
      final mechanism = Mechanism(type: 'runZonedGuarded', handled: true);
      final throwableMechanism = ThrowableMechanism(mechanism, exception);

      final event = SentryEvent(
        throwable: throwableMechanism,
        level: SentryLevel.fatal,
      );

      await hub.captureEvent(event, stackTrace: stackTrace);
    },
    zoneSpecification: ZoneSpecification(
      print: (self, parent, zone, line) {
        if (!options.enablePrintBreadcrumbs || !hub.isEnabled) {
          // early bail out, in order to better guard against the recursion
          // as described below.
          parent.print(zone, line);
          return;
        }

        if (_isPrinting) {
          // We somehow landed in a recursion.
          // This happens for example if:
          // - hub.addBreadcrumb() called print() itself
          // - This happens for example if hub.isEnabled == false and
          //   options.logger == dartLogger
          //
          // Anyway, in order to not cause a stack overflow due to recursion
          // we drop any further print() call while adding a breadcrumb.
          parent.print(
            zone,
            'Recursion during print() call. '
            'Abort adding print() call as Breadcrumb.',
          );
          return;
        }

        _isPrinting = true;

        try {
          hub.addBreadcrumb(Breadcrumb(
            message: line,
            level: SentryLevel.debug,
            category: 'console',
            type: 'debug',
          ));

          parent.print(zone, line);
        } finally {
          _isPrinting = false;
        }
      },
    ),
  );

  options.sdk.addIntegration('runZonedGuardedIntegration');
}