Skip to content

Commit

Permalink
fix(remix): Use captureRemixServerException inside handleError. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Sep 26, 2023
1 parent 952473b commit 0231cd3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/remix/codemods/handle-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions src/remix/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
`;

0 comments on commit 0231cd3

Please sign in to comment.