From 0231cd30a00f6cf2f771d0e16fadacb5b5bdbe90 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Tue, 26 Sep 2023 19:42:53 +0100 Subject: [PATCH] fix(remix): Use `captureRemixServerException` inside `handleError`. (#466) --- CHANGELOG.md | 4 ++++ src/remix/codemods/handle-error.ts | 30 ++++++++++++++++++++++++++++++ src/remix/templates.ts | 8 ++------ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a4cf294..92bbcba1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +fix(remix): Use captureRemixServerException inside handleError. (#466) + ## 3.14.1 ref(sveltekit): Add log for successful Vite plugin insertion (#465) diff --git a/src/remix/codemods/handle-error.ts b/src/remix/codemods/handle-error.ts index af412036..7557b54c 100644 --- a/src/remix/codemods/handle-error.ts +++ b/src/remix/codemods/handle-error.ts @@ -55,12 +55,42 @@ export function instrumentHandleError( ) { return false; } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const implementation = recast.parse(HANDLE_ERROR_TEMPLATE_V2).program + .body[0]; + // @ts-expect-error - string works here because the AST is proxified by magicast // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call handleErrorFunction.declaration.body.body.unshift( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access recast.parse(HANDLE_ERROR_TEMPLATE_V2).program.body[0].body.body[0], ); + + // First parameter is the error + // + // @ts-expect-error - string works here because the AST is proxified by magicast + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + handleErrorFunction.declaration.params[0] = implementation.params[0]; + + // Second parameter is the request inside an object + // Merging the object properties to make sure it includes request + // + // @ts-expect-error - string works here because the AST is proxified by magicast + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + if (handleErrorFunction.declaration.params?.[1]?.properties) { + // @ts-expect-error - string works here because the AST is proxified by magicast + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call + handleErrorFunction.declaration.params[1].properties.push( + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + implementation.params[1].properties[0], + ); + } else { + // Create second parameter if it doesn't exist + // + // @ts-expect-error - string works here because the AST is proxified by magicast + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + handleErrorFunction.declaration.params[1] = implementation.params[1]; + } } return true; diff --git a/src/remix/templates.ts b/src/remix/templates.ts index 6da38899..201b29b7 100644 --- a/src/remix/templates.ts +++ b/src/remix/templates.ts @@ -5,11 +5,7 @@ export const ERROR_BOUNDARY_TEMPLATE_V2 = `const ErrorBoundary = () => { }; `; -export const HANDLE_ERROR_TEMPLATE_V2 = `function handleError(error) { - if (error instanceof Error) { - Sentry.captureRemixErrorBoundaryError(error); - } else { - Sentry.captureException(error); - } +export const HANDLE_ERROR_TEMPLATE_V2 = `function handleError(error, { request }) { + Sentry.captureRemixServerException(error, 'remix.server', { request }); } `;