Skip to content

Commit 5290b95

Browse files
fix(sessions): mechanism.handled:false should crash current session (#3900)
1 parent 0ebca77 commit 5290b95

File tree

8 files changed

+34
-15
lines changed

8 files changed

+34
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- `sentry-expo-upload-sourcemaps` no longer requires Sentry url when uploading sourcemaps to `sentry.io` ([#3915](https://github.com/getsentry/sentry-react-native/pull/3915))
12+
- `mechanism.handled:false` should crash current session ([#3900](https://github.com/getsentry/sentry-react-native/pull/3900))
1213

1314
### Dependencies
1415

android/src/main/java/io/sentry/react/RNSentryModuleImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public void captureEnvelope(String rawBytes, ReadableMap options, Promise promis
398398
byte[] bytes = Base64.decode(rawBytes, Base64.DEFAULT);
399399

400400
try {
401-
InternalSentrySdk.captureEnvelope(bytes, false);
401+
InternalSentrySdk.captureEnvelope(bytes, !options.hasKey("hardCrashed") || !options.getBoolean("hardCrashed"));
402402
} catch (Throwable e) {
403403
logger.log(SentryLevel.ERROR, "Error while capturing envelope");
404404
promise.resolve(false);

ios/RNSentry.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ - (NSDictionary*) fetchNativeStackFramesBy: (NSArray<NSNumber*>*)instructionsAdd
456456
#if DEBUG
457457
[PrivateSentrySDKOnly captureEnvelope:envelope];
458458
#else
459-
if ([[options objectForKey:@"store"] boolValue]) {
459+
if ([[options objectForKey:@"hardCrashed"] boolValue]) {
460460
// Storing to disk happens asynchronously with captureEnvelope
461461
[PrivateSentrySDKOnly storeEnvelope:envelope];
462462
} else {

src/js/NativeRNSentry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface Spec extends TurboModule {
1313
captureEnvelope(
1414
bytes: string,
1515
options: {
16-
store: boolean;
16+
hardCrashed: boolean;
1717
},
1818
): Promise<boolean>;
1919
captureScreenshot(): Promise<NativeScreenshot[] | undefined | null>;

src/js/misc.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ type EnvelopeItemPayload = EnvelopeItem[1];
55
/**
66
* Extracts the hard crash information from the event exceptions.
77
* No exceptions or undefined handled are not hard crashes.
8+
*
9+
* Hard crashes are only unhandled error, not user set unhandled mechanisms.
810
*/
911
export function isHardCrash(payload: EnvelopeItemPayload): boolean {
1012
const values: Exception[] =
1113
typeof payload !== 'string' && 'exception' in payload && payload.exception?.values ? payload.exception.values : [];
1214
for (const exception of values) {
13-
if (!(exception.mechanism?.handled !== false)) {
15+
if (exception.mechanism && exception.mechanism.handled === false && exception.mechanism.type === 'onerror') {
1416
return true;
1517
}
1618
}

src/js/wrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export const NATIVE: SentryNativeWrapper = {
186186
envelopeBytes = newBytes;
187187
}
188188

189-
await RNSentry.captureEnvelope(base64StringFromByteArray(envelopeBytes), { store: hardCrashed });
189+
await RNSentry.captureEnvelope(base64StringFromByteArray(envelopeBytes), { hardCrashed });
190190
},
191191

192192
/**

test/misc.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ describe('misc', () => {
3030
}),
3131
).toBe(false);
3232
});
33+
test('handled false outside of onerror is not a hard crash', () => {
34+
expect(
35+
isHardCrash({
36+
exception: {
37+
values: [
38+
{
39+
mechanism: {
40+
handled: false,
41+
type: 'test',
42+
},
43+
},
44+
],
45+
},
46+
}),
47+
).toBe(false);
48+
});
3349
test('any handled false is a hard crash', () => {
3450
expect(
3551
isHardCrash({
@@ -39,7 +55,7 @@ describe('misc', () => {
3955
{
4056
mechanism: {
4157
handled: false,
42-
type: 'test',
58+
type: 'onerror',
4359
},
4460
},
4561
{

test/wrapper.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ describe('Tests Native Wrapper', () => {
276276
'{"event_id":"event0","message":"test","sdk":{"name":"test-sdk-name","version":"2.1.3"}}\n',
277277
),
278278
),
279-
{ store: false },
279+
{ hardCrashed: false },
280280
);
281281
});
282282
test('serializes class instances', async () => {
@@ -308,7 +308,7 @@ describe('Tests Native Wrapper', () => {
308308
'{"event_id":"event0","sdk":{"name":"test-sdk-name","version":"2.1.3"},"instance":{"value":0}}\n',
309309
),
310310
),
311-
{ store: false },
311+
{ hardCrashed: false },
312312
);
313313
});
314314
test('does not call RNSentry at all if enableNative is false', async () => {
@@ -345,7 +345,7 @@ describe('Tests Native Wrapper', () => {
345345
'{"event_id":"event0","message":{"message":"test"}}\n',
346346
),
347347
),
348-
{ store: false },
348+
{ hardCrashed: false },
349349
);
350350
});
351351
test('Keeps breadcrumbs on Android if mechanism.handled is true', async () => {
@@ -384,7 +384,7 @@ describe('Tests Native Wrapper', () => {
384384
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":true,"type":""}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
385385
),
386386
),
387-
{ store: false },
387+
{ hardCrashed: false },
388388
);
389389
});
390390
test('Keeps breadcrumbs on Android if there is no exception', async () => {
@@ -413,7 +413,7 @@ describe('Tests Native Wrapper', () => {
413413
'{"event_id":"event0","breadcrumbs":[{"message":"crumb!"}]}\n',
414414
),
415415
),
416-
{ store: false },
416+
{ hardCrashed: false },
417417
);
418418
});
419419
test('Keeps breadcrumbs on Android if mechanism.handled is false', async () => {
@@ -426,7 +426,7 @@ describe('Tests Native Wrapper', () => {
426426
{
427427
mechanism: {
428428
handled: false,
429-
type: '',
429+
type: 'onerror',
430430
},
431431
},
432432
],
@@ -448,11 +448,11 @@ describe('Tests Native Wrapper', () => {
448448
base64StringFromByteArray(
449449
utf8ToBytes(
450450
'{"event_id":"event0","sent_at":"123"}\n' +
451-
'{"type":"event","content_type":"application/json","length":125}\n' +
452-
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":false,"type":""}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
451+
'{"type":"event","content_type":"application/json","length":132}\n' +
452+
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":false,"type":"onerror"}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
453453
),
454454
),
455-
{ store: true },
455+
{ hardCrashed: true },
456456
);
457457
});
458458
});

0 commit comments

Comments
 (0)