Skip to content

Commit c1ece00

Browse files
committed
Merge branch 'main' into feat/proxy-setup
2 parents 8452d39 + 7f14ddd commit c1ece00

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
- Add proxy support ([#2192](https://github.com/getsentry/sentry-dart/pull/2192))
88

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

1114
- Disable sff & frame delay detection on web, linux and windows ([#2182](https://github.com/getsentry/sentry-dart/pull/2182))

dart/lib/src/sentry_client.dart

+18-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SentryClient {
4545

4646
late final MetricsAggregator? _metricsAggregator;
4747

48-
static final _sentryId = Future.value(SentryId.empty());
48+
static final _emptySentryId = Future.value(SentryId.empty());
4949

5050
SentryExceptionFactory get _exceptionFactory => _options.exceptionFactory;
5151

@@ -83,14 +83,24 @@ class SentryClient {
8383
dynamic stackTrace,
8484
Hint? hint,
8585
}) async {
86+
if (_options.containsIgnoredExceptionForType(event.throwable)) {
87+
_options.logger(
88+
SentryLevel.debug,
89+
'Event was dropped as the exception ${event.throwable.runtimeType.toString()} is ignored.',
90+
);
91+
_options.recorder
92+
.recordLostEvent(DiscardReason.eventProcessor, _getCategory(event));
93+
return _emptySentryId;
94+
}
95+
8696
if (_sampleRate()) {
8797
_options.recorder
8898
.recordLostEvent(DiscardReason.sampleRate, _getCategory(event));
8999
_options.logger(
90100
SentryLevel.debug,
91101
'Event ${event.eventId.toString()} was dropped due to sampling decision.',
92102
);
93-
return _sentryId;
103+
return _emptySentryId;
94104
}
95105

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

107117
// dropped by scope event processors
108118
if (preparedEvent == null) {
109-
return _sentryId;
119+
return _emptySentryId;
110120
}
111121

112122
preparedEvent = await _runEventProcessors(
@@ -117,7 +127,7 @@ class SentryClient {
117127

118128
// dropped by event processors
119129
if (preparedEvent == null) {
120-
return _sentryId;
130+
return _emptySentryId;
121131
}
122132

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

130140
// dropped by beforeSend
131141
if (preparedEvent == null) {
132-
return _sentryId;
142+
return _emptySentryId;
133143
}
134144

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

327337
// dropped by scope event processors
328338
if (preparedTransaction == null) {
329-
return _sentryId;
339+
return _emptySentryId;
330340
}
331341

332342
preparedTransaction = await _runEventProcessors(
@@ -337,15 +347,15 @@ class SentryClient {
337347

338348
// dropped by event processors
339349
if (preparedTransaction == null) {
340-
return _sentryId;
350+
return _emptySentryId;
341351
}
342352

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

346356
// dropped by beforeSendTransaction
347357
if (preparedTransaction == null) {
348-
return _sentryId;
358+
return _emptySentryId;
349359
}
350360

351361
final attachments = scope?.attachments

dart/lib/src/sentry_options.dart

+16
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,22 @@ class SentryOptions {
334334
_scopeObservers.add(scopeObserver);
335335
}
336336

337+
final List<Type> _ignoredExceptionsForType = [];
338+
339+
/// Ignored exception types.
340+
List<Type> get ignoredExceptionsForType => _ignoredExceptionsForType;
341+
342+
/// Adds exception type to the list of ignored exceptions.
343+
void addExceptionFilterForType(Type exceptionType) {
344+
_ignoredExceptionsForType.add(exceptionType);
345+
}
346+
347+
/// Check if [ignoredExceptionsForType] contains an exception.
348+
bool containsIgnoredExceptionForType(dynamic exception) {
349+
return exception != null &&
350+
_ignoredExceptionsForType.contains(exception.runtimeType);
351+
}
352+
337353
@internal
338354
late ClientReportRecorder recorder = NoOpClientReportRecorder();
339355

dart/test/sentry_client_test.dart

+36
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,42 @@ void main() {
10331033
});
10341034
});
10351035

1036+
group('SentryClient ignored exceptions', () {
1037+
late Fixture fixture;
1038+
1039+
setUp(() {
1040+
fixture = Fixture();
1041+
});
1042+
1043+
test('addExceptionFilterForType drops matching error event throwable',
1044+
() async {
1045+
fixture.options.addExceptionFilterForType(ExceptionWithCause);
1046+
1047+
final throwable = ExceptionWithCause(Error(), StackTrace.current);
1048+
final event = SentryEvent(throwable: throwable);
1049+
1050+
final client = fixture.getSut();
1051+
await client.captureEvent(event);
1052+
1053+
expect((fixture.transport).called(0), true);
1054+
});
1055+
1056+
test('record ignored exceptions dropping event', () async {
1057+
fixture.options.addExceptionFilterForType(ExceptionWithCause);
1058+
1059+
final throwable = ExceptionWithCause(Error(), StackTrace.current);
1060+
final event = SentryEvent(throwable: throwable);
1061+
1062+
final client = fixture.getSut();
1063+
await client.captureEvent(event);
1064+
1065+
expect(fixture.recorder.discardedEvents.first.reason,
1066+
DiscardReason.eventProcessor);
1067+
expect(
1068+
fixture.recorder.discardedEvents.first.category, DataCategory.error);
1069+
});
1070+
});
1071+
10361072
group('SentryClient before send transaction', () {
10371073
late Fixture fixture;
10381074

0 commit comments

Comments
 (0)