Skip to content

Commit 16450b8

Browse files
fix(sessions): Missing mechanism.handled is not considered crash (#3353)
Some events have mechanism.handled missing in events.exceptions.values and those events should not crash the session. This happens for example in Flutter.
1 parent 209e288 commit 16450b8

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Missing `mechanism.handled` is not considered crash (#3353)
8+
39
## 8.14.1
410

511
### Fixes

Sources/Sentry/SentryHub.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,22 +642,22 @@ - (BOOL)envelopeContainsEventWithErrorOrHigher:(NSArray<SentryEnvelopeItem *> *)
642642

643643
SentryLevel level = sentryLevelForString(eventJson[@"level"]);
644644
if (level >= kSentryLevelError) {
645-
*handled = [self eventContainsUnhandledError:eventJson];
645+
*handled = [self eventContainsOnlyHandledErrors:eventJson];
646646
return YES;
647647
}
648648
}
649649
}
650650
return NO;
651651
}
652652

653-
- (BOOL)eventContainsUnhandledError:(NSDictionary *)eventDictionary
653+
- (BOOL)eventContainsOnlyHandledErrors:(NSDictionary *)eventDictionary
654654
{
655655
NSArray *exceptions = eventDictionary[@"exception"][@"values"];
656656
for (NSDictionary *exception in exceptions) {
657657
NSDictionary *mechanism = exception[@"mechanism"];
658658
NSNumber *handled = mechanism[@"handled"];
659659

660-
if ([handled boolValue] == NO) {
660+
if (handled != nil && [handled boolValue] == NO) {
661661
return NO;
662662
}
663663
}

Tests/SentryTests/SentryHubTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Sentry
22
import SentryTestUtils
33
import XCTest
44

5+
// swiftlint:disable file_length
56
class SentryHubTests: XCTestCase {
67

78
private static let dsnAsString = TestConstants.dsnAsString(username: "SentryHubTests")
@@ -656,6 +657,14 @@ class SentryHubTests: XCTestCase {
656657
assertSessionWithIncrementedErrorCountedAdded()
657658
}
658659

660+
func testCaptureEnvelope_WithEventWithoutExceptionMechanism() {
661+
sut.startSession()
662+
663+
captureFatalEventWithoutExceptionMechanism()
664+
665+
assertSessionWithIncrementedErrorCountedAdded()
666+
}
667+
659668
func testCaptureEnvelope_WithEventWithFatal() {
660669
sut.startSession()
661670

@@ -864,12 +873,49 @@ class SentryHubTests: XCTestCase {
864873
group.wait()
865874
}
866875

876+
func testEventContainsOnlyHandledErrors() {
877+
let sut = fixture.getSut()
878+
XCTAssertFalse(sut.eventContainsOnlyHandledErrors(["exception":
879+
["values":
880+
[["mechanism": ["handled": false]]]
881+
]
882+
]))
883+
884+
XCTAssertTrue(sut.eventContainsOnlyHandledErrors(["exception":
885+
["values":
886+
[["mechanism": ["handled": true]],
887+
["mechanism": ["handled": true]]]
888+
]
889+
]))
890+
891+
XCTAssertFalse(sut.eventContainsOnlyHandledErrors(["exception":
892+
["values":
893+
[["mechanism": ["handled": true]],
894+
["mechanism": ["handled": false]]]
895+
]
896+
]))
897+
898+
XCTAssertTrue(sut.eventContainsOnlyHandledErrors(["exception":
899+
["values":
900+
[["mechanism": ["handled": true]],
901+
["mechanism": ["other-key": false]]]
902+
]
903+
]))
904+
}
905+
867906
private func captureEventEnvelope(level: SentryLevel) {
868907
let event = TestData.event
869908
event.level = level
870909
sut.capture(SentryEnvelope(event: event))
871910
}
872911

912+
private func captureFatalEventWithoutExceptionMechanism() {
913+
let event = TestData.event
914+
event.level = SentryLevel.fatal
915+
event.exceptions?[0].mechanism = nil
916+
sut.capture(SentryEnvelope(event: event))
917+
}
918+
873919
private func givenCrashedSession() {
874920
fixture.sentryCrash.internalCrashedLastLaunch = true
875921
fixture.fileManager.storeCrashedSession(fixture.crashedSession)

Tests/SentryTests/State/SentryHub+Test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ SentryHub ()
1616
- (NSArray<id<SentryIntegrationProtocol>> *)installedIntegrations;
1717
- (NSSet<NSString *> *)installedIntegrationNames;
1818

19+
- (BOOL)eventContainsOnlyHandledErrors:(NSDictionary *)eventDictionary;
1920
@end
2021

2122
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)