Skip to content

Commit

Permalink
Support ignoredExceptionsForType (#2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase authored Jul 23, 2024
1 parent 7770462 commit 7f14ddd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Features

- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150))
- Filter out exception types by calling `SentryOptions.addExceptionFilterForType(Type exceptionType)`

### Fixes

- Disable sff & frame delay detection on web, linux and windows ([#2182](https://github.com/getsentry/sentry-dart/pull/2182))
Expand Down
26 changes: 18 additions & 8 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SentryClient {

late final MetricsAggregator? _metricsAggregator;

static final _sentryId = Future.value(SentryId.empty());
static final _emptySentryId = Future.value(SentryId.empty());

SentryExceptionFactory get _exceptionFactory => _options.exceptionFactory;

Expand Down Expand Up @@ -83,14 +83,24 @@ class SentryClient {
dynamic stackTrace,
Hint? hint,
}) async {
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()) {
_options.recorder
.recordLostEvent(DiscardReason.sampleRate, _getCategory(event));
_options.logger(
SentryLevel.debug,
'Event ${event.eventId.toString()} was dropped due to sampling decision.',
);
return _sentryId;
return _emptySentryId;
}

SentryEvent? preparedEvent = _prepareEvent(event, stackTrace: stackTrace);
Expand All @@ -106,7 +116,7 @@ class SentryClient {

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

preparedEvent = await _runEventProcessors(
Expand All @@ -117,7 +127,7 @@ class SentryClient {

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

preparedEvent = _createUserOrSetDefaultIpAddress(preparedEvent);
Expand All @@ -129,7 +139,7 @@ class SentryClient {

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

var attachments = List<SentryAttachment>.from(scope?.attachments ?? []);
Expand Down Expand Up @@ -326,7 +336,7 @@ class SentryClient {

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

preparedTransaction = await _runEventProcessors(
Expand All @@ -337,15 +347,15 @@ class SentryClient {

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

preparedTransaction =
await _runBeforeSend(preparedTransaction, hint) as SentryTransaction?;

// dropped by beforeSendTransaction
if (preparedTransaction == null) {
return _sentryId;
return _emptySentryId;
}

final attachments = scope?.attachments
Expand Down
16 changes: 16 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ class SentryOptions {
_scopeObservers.add(scopeObserver);
}

final List<Type> _ignoredExceptionsForType = [];

/// Ignored exception types.
List<Type> get ignoredExceptionsForType => _ignoredExceptionsForType;

/// Adds exception type to the list of ignored exceptions.
void addExceptionFilterForType(Type exceptionType) {
_ignoredExceptionsForType.add(exceptionType);
}

/// Check if [ignoredExceptionsForType] contains an exception.
bool containsIgnoredExceptionForType(dynamic exception) {
return exception != null &&
_ignoredExceptionsForType.contains(exception.runtimeType);
}

@internal
late ClientReportRecorder recorder = NoOpClientReportRecorder();

Expand Down
36 changes: 36 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,42 @@ void main() {
});
});

group('SentryClient ignored exceptions', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('addExceptionFilterForType drops matching error event throwable',
() async {
fixture.options.addExceptionFilterForType(ExceptionWithCause);

final throwable = ExceptionWithCause(Error(), StackTrace.current);
final event = SentryEvent(throwable: throwable);

final client = fixture.getSut();
await client.captureEvent(event);

expect((fixture.transport).called(0), true);
});

test('record ignored exceptions dropping event', () async {
fixture.options.addExceptionFilterForType(ExceptionWithCause);

final throwable = ExceptionWithCause(Error(), StackTrace.current);
final event = SentryEvent(throwable: throwable);

final client = fixture.getSut();
await client.captureEvent(event);

expect(fixture.recorder.discardedEvents.first.reason,
DiscardReason.eventProcessor);
expect(
fixture.recorder.discardedEvents.first.category, DataCategory.error);
});
});

group('SentryClient before send transaction', () {
late Fixture fixture;

Expand Down

0 comments on commit 7f14ddd

Please sign in to comment.