captureEvent method

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

Reports an event to Sentry.io.

Implementation

Future<SentryId> captureEvent(
  SentryEvent event, {
  Scope? scope,
  dynamic stackTrace,
  Hint? hint,
}) async {
  if (_isIgnoredError(event)) {
    _options.logger(
      SentryLevel.debug,
      'Error was ignored as specified in the ignoredErrors options.',
    );
    _options.recorder
        .recordLostEvent(DiscardReason.ignored, _getCategory(event));
    return _emptySentryId;
  }

  if (_options.containsIgnoredExceptionForType(event.throwable)) {
    _options.logger(
      SentryLevel.debug,
      'Event was dropped as the exception ${event.throwable.runtimeType.toString()} is ignored.',
    );
    _options.recorder
        .recordLostEvent(DiscardReason.eventProcessor, _getCategory(event));
    return _emptySentryId;
  }

  if (_sampleRate() && event.type != 'feedback') {
    _options.recorder
        .recordLostEvent(DiscardReason.sampleRate, _getCategory(event));
    _options.logger(
      SentryLevel.debug,
      'Event ${event.eventId.toString()} was dropped due to sampling decision.',
    );
    return _emptySentryId;
  }

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

  hint ??= Hint();

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

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

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

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

  preparedEvent = _createUserOrSetDefaultIpAddress(preparedEvent);

  preparedEvent = await _runBeforeSend(
    preparedEvent,
    hint,
  );

  // dropped by beforeSend
  if (preparedEvent == null) {
    return _emptySentryId;
  }

  var attachments = List<SentryAttachment>.from(scope?.attachments ?? []);
  attachments.addAll(hint.attachments);
  var screenshot = hint.screenshot;
  if (screenshot != null) {
    attachments.add(screenshot);
  }

  var viewHierarchy = hint.viewHierarchy;
  if (viewHierarchy != null && event.type != 'feedback') {
    attachments.add(viewHierarchy);
  }

  var traceContext = scope?.span?.traceContext();
  if (traceContext == null) {
    if (scope != null) {
      scope.propagationContext.baggage ??=
          SentryBaggage({}, logger: _options.logger)
            ..setValuesFromScope(scope, _options);
      traceContext = SentryTraceContextHeader.fromBaggage(
          scope.propagationContext.baggage!);
    }
  } else {
    traceContext.replayId = scope?.replayId;
  }

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

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