call method

  1. @override
void call(
  1. Hub hub,
  2. SentryFlutterOptions options
)
override

A Callable method for the Integration interface

Implementation

@override
void call(Hub hub, SentryFlutterOptions options) {
  _options = options;
  final binding = options.bindingUtils.instance;

  if (binding == null) {
    return;
  }

  // WidgetsBinding works with WidgetsFlutterBinding and other custom bindings
  final wrapper = dispatchWrapper ??
      PlatformDispatcherWrapper(binding.platformDispatcher);

  _defaultOnError = wrapper.onError;

  _integrationOnError = (Object exception, StackTrace stackTrace) {
    _options!.logger(
      SentryLevel.error,
      "Uncaught Platform Error",
      logger: 'sentry.platformError',
      exception: exception,
      stackTrace: stackTrace,
    );

    final handled = _defaultOnError?.call(exception, stackTrace) ?? true;

    // As per docs, the app might crash on some platforms
    // after this is called.
    // https://master-api.flutter.dev/flutter/dart-ui/PlatformDispatcher/onError.html
    // https://master-api.flutter.dev/flutter/dart-ui/ErrorCallback.html
    final mechanism = Mechanism(
      type: 'PlatformDispatcher.onError',
      handled: handled,
    );
    final throwableMechanism = ThrowableMechanism(mechanism, exception);

    var event = SentryEvent(
      throwable: throwableMechanism,
      level: SentryLevel.fatal,
      // ignore: invalid_use_of_internal_member
      timestamp: options.clock(),
    );

    // marks the span status if none to `internal_error` in case there's an
    // unhandled error
    hub.configureScope(
      (scope) => scope.span?.status ??= const SpanStatus.internalError(),
    );

    // unawaited future
    hub.captureEvent(event, stackTrace: stackTrace);

    return handled;
  };

  wrapper.onError = _integrationOnError;
  dispatchWrapper = wrapper;

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