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

fix(sessions): mechanism.handled:false should crash current session #3900

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- `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))
- `mechanism.handled:false` should crash current session ([#3900](https://github.com/getsentry/sentry-react-native/pull/3900))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public void captureEnvelope(String rawBytes, ReadableMap options, Promise promis
byte[] bytes = Base64.decode(rawBytes, Base64.DEFAULT);

try {
InternalSentrySdk.captureEnvelope(bytes, false);
InternalSentrySdk.captureEnvelope(bytes, !options.hasKey("hardCrashed") || !options.getBoolean("hardCrashed"));
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Error while capturing envelope");
promise.resolve(false);
Expand Down
2 changes: 1 addition & 1 deletion ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ - (NSDictionary*) fetchNativeStackFramesBy: (NSArray<NSNumber*>*)instructionsAdd
#if DEBUG
[PrivateSentrySDKOnly captureEnvelope:envelope];
#else
if ([[options objectForKey:@"store"] boolValue]) {
if ([[options objectForKey:@"hardCrashed"] boolValue]) {
// Storing to disk happens asynchronously with captureEnvelope
[PrivateSentrySDKOnly storeEnvelope:envelope];
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/js/NativeRNSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Spec extends TurboModule {
captureEnvelope(
bytes: string,
options: {
store: boolean;
hardCrashed: boolean;
},
): Promise<boolean>;
captureScreenshot(): Promise<NativeScreenshot[] | undefined | null>;
Expand Down
4 changes: 3 additions & 1 deletion src/js/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ type EnvelopeItemPayload = EnvelopeItem[1];
/**
* Extracts the hard crash information from the event exceptions.
* No exceptions or undefined handled are not hard crashes.
*
* Hard crashes are only unhandled error, not user set unhandled mechanisms.
*/
export function isHardCrash(payload: EnvelopeItemPayload): boolean {
const values: Exception[] =
typeof payload !== 'string' && 'exception' in payload && payload.exception?.values ? payload.exception.values : [];
for (const exception of values) {
if (!(exception.mechanism?.handled !== false)) {
if (exception.mechanism && exception.mechanism.handled === false && exception.mechanism.type === 'onerror') {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const NATIVE: SentryNativeWrapper = {
envelopeBytes = newBytes;
}

await RNSentry.captureEnvelope(base64StringFromByteArray(envelopeBytes), { store: hardCrashed });
await RNSentry.captureEnvelope(base64StringFromByteArray(envelopeBytes), { hardCrashed });
},

/**
Expand Down
18 changes: 17 additions & 1 deletion test/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('misc', () => {
}),
).toBe(false);
});
test('handled false outside of onerror is not a hard crash', () => {
expect(
isHardCrash({
exception: {
values: [
{
mechanism: {
handled: false,
type: 'test',
},
},
],
},
}),
).toBe(false);
});
test('any handled false is a hard crash', () => {
expect(
isHardCrash({
Expand All @@ -39,7 +55,7 @@ describe('misc', () => {
{
mechanism: {
handled: false,
type: 'test',
type: 'onerror',
},
},
{
Expand Down
18 changes: 9 additions & 9 deletions test/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe('Tests Native Wrapper', () => {
'{"event_id":"event0","message":"test","sdk":{"name":"test-sdk-name","version":"2.1.3"}}\n',
),
),
{ store: false },
{ hardCrashed: false },
);
});
test('serializes class instances', async () => {
Expand Down Expand Up @@ -308,7 +308,7 @@ describe('Tests Native Wrapper', () => {
'{"event_id":"event0","sdk":{"name":"test-sdk-name","version":"2.1.3"},"instance":{"value":0}}\n',
),
),
{ store: false },
{ hardCrashed: false },
);
});
test('does not call RNSentry at all if enableNative is false', async () => {
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('Tests Native Wrapper', () => {
'{"event_id":"event0","message":{"message":"test"}}\n',
),
),
{ store: false },
{ hardCrashed: false },
);
});
test('Keeps breadcrumbs on Android if mechanism.handled is true', async () => {
Expand Down Expand Up @@ -384,7 +384,7 @@ describe('Tests Native Wrapper', () => {
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":true,"type":""}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
),
),
{ store: false },
{ hardCrashed: false },
);
});
test('Keeps breadcrumbs on Android if there is no exception', async () => {
Expand Down Expand Up @@ -413,7 +413,7 @@ describe('Tests Native Wrapper', () => {
'{"event_id":"event0","breadcrumbs":[{"message":"crumb!"}]}\n',
),
),
{ store: false },
{ hardCrashed: false },
);
});
test('Keeps breadcrumbs on Android if mechanism.handled is false', async () => {
Expand All @@ -426,7 +426,7 @@ describe('Tests Native Wrapper', () => {
{
mechanism: {
handled: false,
type: '',
type: 'onerror',
},
},
],
Expand All @@ -448,11 +448,11 @@ describe('Tests Native Wrapper', () => {
base64StringFromByteArray(
utf8ToBytes(
'{"event_id":"event0","sent_at":"123"}\n' +
'{"type":"event","content_type":"application/json","length":125}\n' +
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":false,"type":""}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
'{"type":"event","content_type":"application/json","length":132}\n' +
'{"event_id":"event0","exception":{"values":[{"mechanism":{"handled":false,"type":"onerror"}}]},"breadcrumbs":[{"message":"crumb!"}]}\n',
),
),
{ store: true },
{ hardCrashed: true },
);
});
});
Expand Down
Loading