Skip to content

Support ignoredExceptionsForType #2150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@
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 @@

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

Check warning on line 119 in dart/lib/src/sentry_client.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/sentry_client.dart#L119

Added line #L119 was not covered by tests
}

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

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

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

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

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

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

Check warning on line 340 in dart/lib/src/sentry_client.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/sentry_client.dart#L339-L340

Added lines #L339 - L340 were not covered by tests

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

// 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
Loading