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 committed Sep 21, 2023
1 parent 952473b commit 3af2781
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/remix/codemods/handle-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,41 @@ 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]?.parametes) {
// @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].parametes.push(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
implementation.params[1].parametes[0],
);
}

// Update parameters
// @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 = implementation.params;
}

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 3af2781

Please sign in to comment.