Skip to content

Commit 2642813

Browse files
committed
Align codemods and fallback templates
1 parent 1033e59 commit 2642813

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/react-router/codemods/root.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ function hasCaptureExceptionCall(node: t.Node): boolean {
4242
}
4343

4444
function addCaptureExceptionCall(functionNode: t.Node): void {
45-
const captureExceptionCall = recast.parse(`Sentry.captureException(error);`)
46-
.program.body[0];
45+
const captureExceptionCall = recast.parse(
46+
`if (error && error instanceof Error) {\n Sentry.captureException(error);\n}`,
47+
).program.body[0];
4748

4849
const functionBody = safeGetFunctionBody(functionNode);
4950
if (functionBody) {

src/react-router/templates.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ export default defineConfig(config => {
436436
authToken: process.env.SENTRY_AUTH_TOKEN,
437437
}, config),`)}
438438
],
439+
${plus(`optimizeDeps: {
440+
exclude: ['@sentry/react-router'],
441+
},`)}
439442
};
440443
});`),
441444
);

test/react-router/codemods/root.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('instrumentRoot', () => {
9898
'import * as Sentry from "@sentry/react-router";',
9999
);
100100
expect(modifiedContent).toContain('Sentry.captureException(error);');
101+
expect(modifiedContent).toContain('error instanceof Error');
101102
});
102103

103104
it('should add Sentry.captureException to existing variable declaration ErrorBoundary', async () => {
@@ -117,6 +118,7 @@ describe('instrumentRoot', () => {
117118
);
118119
// Now properly handles variable declaration ErrorBoundary
119120
expect(modifiedContent).toContain('Sentry.captureException(error);');
121+
expect(modifiedContent).toContain('error instanceof Error');
120122
});
121123

122124
it('should not modify file when ErrorBoundary already has Sentry.captureException', async () => {
@@ -215,6 +217,7 @@ describe('instrumentRoot', () => {
215217
'import * as Sentry from "@sentry/react-router";',
216218
);
217219
expect(modifiedContent).toContain('Sentry.captureException(error);');
220+
expect(modifiedContent).toContain('error instanceof Error');
218221
});
219222

220223
it('should handle function declaration with separate export', async () => {
@@ -236,6 +239,7 @@ describe('instrumentRoot', () => {
236239
'import * as Sentry from "@sentry/react-router";',
237240
);
238241
expect(modifiedContent).toContain('Sentry.captureException(error);');
242+
expect(modifiedContent).toContain('error instanceof Error');
239243

240244
// Should preserve function declaration syntax
241245
expect(modifiedContent).toMatch(/function ErrorBoundary\(/);

0 commit comments

Comments
 (0)