Skip to content
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

Support ignoredExceptionsForType #2150

Merged
merged 13 commits into from
Jul 23, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Add memory usage to contexts ([#2133](https://github.com/getsentry/sentry-dart/pull/2133))
- Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs
- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150))

### Fixes

Expand Down
10 changes: 10 additions & 0 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ 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.',
);

_recordLostEvent(event, DiscardReason.eventProcessor);
return _sentryId;
denrase marked this conversation as resolved.
Show resolved Hide resolved
}

if (_sampleRate()) {
_recordLostEvent(event, DiscardReason.sampleRate);
_options.logger(
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 @@
_scopeObservers.add(scopeObserver);
}

final List<Type> _ignoredExceptionsForType = [];

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

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

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/sentry_options.dart#L340

Added line #L340 was not covered by tests

/// 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);
denrase marked this conversation as resolved.
Show resolved Hide resolved
}

@internal
late ClientReportRecorder recorder = NoOpClientReportRecorder();

Expand Down
34 changes: 34 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,40 @@ 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.reason, DiscardReason.eventProcessor);
expect(fixture.recorder.category, DataCategory.error);
});
});

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

Expand Down
Loading