captureEvent method

Future<SentryId> captureEvent(
  1. SentryEvent event, {
  2. Scope? scope,
  3. dynamic stackTrace,
  4. dynamic hint,
})

Reports an event to Sentry.io.

Implementation

Future<SentryId> captureEvent(
  SentryEvent event, {
  Scope? scope,
  dynamic stackTrace,
  dynamic hint,
}) async {
  if (_sampleRate()) {
    _recordLostEvent(event, DiscardReason.sampleRate);
    _options.logger(
      SentryLevel.debug,
      'Event ${event.eventId.toString()} was dropped due to sampling decision.',
    );
    return _sentryId;
  }

  SentryEvent? preparedEvent = _prepareEvent(event, stackTrace: stackTrace);

  if (scope != null) {
    preparedEvent = await scope.applyToEvent(preparedEvent, hint: hint);
  } else {
    _options.logger(
        SentryLevel.debug, 'No scope to apply on event was provided');
  }

  // dropped by scope event processors
  if (preparedEvent == null) {
    return _sentryId;
  }

  preparedEvent = await _processEvent(
    preparedEvent,
    eventProcessors: _options.eventProcessors,
    hint: hint,
  );

  // dropped by event processors
  if (preparedEvent == null) {
    return _sentryId;
  }

  final beforeSend = _options.beforeSend;
  if (beforeSend != null) {
    final beforeSendEvent = preparedEvent;
    try {
      preparedEvent = await beforeSend(preparedEvent, hint: hint);
    } catch (exception, stackTrace) {
      _options.logger(
        SentryLevel.error,
        'The BeforeSend callback threw an exception',
        exception: exception,
        stackTrace: stackTrace,
      );
    }
    if (preparedEvent == null) {
      _recordLostEvent(beforeSendEvent, DiscardReason.beforeSend);
      _options.logger(
        SentryLevel.debug,
        'Event was dropped by BeforeSend callback',
      );
      return _sentryId;
    }
  }

  if (_options.platformChecker.platform.isAndroid &&
      _options.enableScopeSync) {
    /*
    We do this to avoid duplicate breadcrumbs on Android as sentry-android applies the breadcrumbs
    from the native scope onto every envelope sent through it. This scope will contain the breadcrumbs
    sent through the scope sync feature. This causes duplicate breadcrumbs.
    We then remove the breadcrumbs in all cases but if it is handled == false,
    this is a signal that the app would crash and android would lose the breadcrumbs by the time the app is restarted to read
    the envelope.
    */
    preparedEvent = _eventWithRemovedBreadcrumbsIfHandled(preparedEvent);
  }

  final attachments = await _clientAttachmentProcessor.processAttachments(
      scope?.attachments ?? [], preparedEvent);

  final envelope = SentryEnvelope.fromEvent(
    preparedEvent,
    _options.sdk,
    dsn: _options.dsn,
    traceContext: scope?.span?.traceContext(),
    attachments: attachments.isNotEmpty ? attachments : null,
  );

  final id = await captureEnvelope(envelope);
  return id ?? SentryId.empty();
}