Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -10,6 +10,7 @@

### Fixes

- Use React `componentStack` as fallback when error has no stack trace on Android ([#5071](https://github.com/getsentry/sentry-react-native/issues/5071))
- Add `SENTRY_PROJECT_ROOT` env var to override project root in Xcode build phase scripts for monorepo setups ([#5961](https://github.com/getsentry/sentry-react-native/pull/5961))

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ function setupErrorUtilsGlobalHandler(): void {
return;
}

// React render errors may arrive without a .stack but with a .componentStack
// (set by ReactFiberErrorDialog). Use the componentStack as a fallback so
// eventFromException can extract frames with source locations.
// oxlint-disable-next-line typescript-eslint(no-unsafe-member-access)
if (!error.stack && error.componentStack) {
// oxlint-disable-next-line typescript-eslint(no-unsafe-member-access)
error.stack = `${error.message || 'Error'}${error.componentStack}`;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we add an extra space between the parameters, so Error is not clued to the componentStack message?

}

const hint: EventHint = {
originalException: error,
attachments: getCurrentScope().getScopeData().attachments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,40 @@ describe('ReactNativeErrorHandlers', () => {

expect(hint).toEqual(expect.objectContaining({ originalException: new Error('Test Error') }));
});

test('Uses componentStack as fallback when error has no stack', async () => {
const integration = reactNativeErrorHandlersIntegration();
integration.setupOnce!();

const error: any = {
message: 'Value is undefined, expected an Object',
componentStack:
'\n at UserMessage (http://localhost:8081/index.bundle:1:5274251)' +
'\n at renderItem (http://localhost:8081/index.bundle:1:5280705)',
};

await errorHandlerCallback!(error, true);
await client.flush();

expect(error.stack).toBe(
'Value is undefined, expected an Object' +
'\n at UserMessage (http://localhost:8081/index.bundle:1:5274251)' +
'\n at renderItem (http://localhost:8081/index.bundle:1:5280705)',
);
});

test('Does not override stack when error already has one', async () => {
const integration = reactNativeErrorHandlersIntegration();
integration.setupOnce!();

const error = new Error('Test Error');
(error as any).componentStack = '\n at SomeComponent (http://localhost:8081/index.bundle:1:100)';
const originalStack = error.stack;

await errorHandlerCallback!(error, false);

expect(error.stack).toBe(originalStack);
});
});

describe('onUnhandledRejection', () => {
Expand Down
Loading