Skip to content

Commit

Permalink
Fix: Duplicated Android Breadcrumbs with no Mechanism (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jul 22, 2022
1 parent 5a205e7 commit 59ffb07
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* Maps with Key Object, Object would fail during serialization if not String, Object ([#935](https://github.com/getsentry/sentry-dart/pull/935))
* Breadcrumbs "Concurrent Modification" ([#948](https://github.com/getsentry/sentry-dart/pull/948))
* Duplicative Screen size changed breadcrumbs ([#888](https://github.com/getsentry/sentry-dart/pull/888))
* Duplicated Android Breadcrumbs with no Mechanism ([#954](https://github.com/getsentry/sentry-dart/pull/954))
* Fix windows native method need default result ([#943](https://github.com/getsentry/sentry-dart/pull/943))

### Features
Expand Down
11 changes: 6 additions & 5 deletions dart/lib/src/sentry_client.dart
Expand Up @@ -378,12 +378,13 @@ class SentryClient {
}

SentryEvent _eventWithRemovedBreadcrumbsIfHandled(SentryEvent event) {
final exceptions = event.exceptions ?? [];
final handled = exceptions.isNotEmpty
? exceptions.first.mechanism?.handled == true
: false;
final mechanisms =
(event.exceptions ?? []).map((e) => e.mechanism).whereType<Mechanism>();
final hasNoMechanism = mechanisms.isEmpty;
final hasOnlyHandledMechanism =
mechanisms.every((e) => (e.handled ?? true));

if (handled) {
if (hasNoMechanism || hasOnlyHandledMechanism) {
return event.copyWith(breadcrumbs: []);
} else {
return event;
Expand Down
7 changes: 4 additions & 3 deletions dart/test/mocks/mock_platform.dart
Expand Up @@ -9,9 +9,10 @@ class MockPlatform extends Platform with NoSuchMethodProvider {
return MockPlatform(os: 'android');
}

@override
String operatingSystem;
factory MockPlatform.iOS() {
return MockPlatform(os: 'ios');
}

@override
bool get isAndroid => (operatingSystem == 'android');
String operatingSystem;
}
85 changes: 84 additions & 1 deletion dart/test/sentry_client_test.dart
Expand Up @@ -898,6 +898,52 @@ void main() {
expect((capturedEvent.breadcrumbs ?? []).isEmpty, true);
});

test('Clears breadcrumbs on Android if mechanism.handled is null',
() async {
fixture.options.enableScopeSync = true;
fixture.options.platformChecker =
MockPlatformChecker(platform: MockPlatform.android());

final client = fixture.getSut();
final event = SentryEvent(exceptions: [
SentryException(
type: "type",
value: "value",
mechanism: Mechanism(type: 'type'),
)
], breadcrumbs: [
Breadcrumb()
]);
await client.captureEvent(event);

final capturedEnvelope = (fixture.transport).envelopes.first;
final capturedEvent = await eventFromEnvelope(capturedEnvelope);

expect((capturedEvent.breadcrumbs ?? []).isEmpty, true);
});

test('Clears breadcrumbs on Android if theres no mechanism', () async {
fixture.options.enableScopeSync = true;
fixture.options.platformChecker =
MockPlatformChecker(platform: MockPlatform.android());

final client = fixture.getSut();
final event = SentryEvent(exceptions: [
SentryException(
type: "type",
value: "value",
)
], breadcrumbs: [
Breadcrumb()
]);
await client.captureEvent(event);

final capturedEnvelope = (fixture.transport).envelopes.first;
final capturedEvent = await eventFromEnvelope(capturedEnvelope);

expect((capturedEvent.breadcrumbs ?? []).isEmpty, true);
});

test('Does not clear breadcrumbs on Android if mechanism.handled is false',
() async {
fixture.options.enableScopeSync = true;
Expand All @@ -924,6 +970,42 @@ void main() {

expect((capturedEvent.breadcrumbs ?? []).isNotEmpty, true);
});

test(
'Does not clear breadcrumbs on Android if any mechanism.handled is false',
() async {
fixture.options.enableScopeSync = true;
fixture.options.platformChecker =
MockPlatformChecker(platform: MockPlatform.android());

final client = fixture.getSut();
final event = SentryEvent(exceptions: [
SentryException(
type: "type",
value: "value",
mechanism: Mechanism(
type: 'type',
handled: true,
),
),
SentryException(
type: "type",
value: "value",
mechanism: Mechanism(
type: 'type',
handled: false,
),
)
], breadcrumbs: [
Breadcrumb()
]);
await client.captureEvent(event);

final capturedEnvelope = (fixture.transport).envelopes.first;
final capturedEvent = await eventFromEnvelope(capturedEnvelope);

expect((capturedEvent.breadcrumbs ?? []).isNotEmpty, true);
});
});

group('ClientReportRecorder', () {
Expand Down Expand Up @@ -1118,7 +1200,8 @@ class Fixture {
final recorder = MockClientReportRecorder();
final transport = MockTransport();

final options = SentryOptions(dsn: fakeDsn);
final options = SentryOptions(dsn: fakeDsn)
..platformChecker = MockPlatformChecker(platform: MockPlatform.iOS());

late SentryTransactionContext _context;
late SentryTracer tracer;
Expand Down

0 comments on commit 59ffb07

Please sign in to comment.