Skip to content

Commit

Permalink
feat(remix): Add wrapHandleErrorWithSentry
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jan 26, 2024
1 parent adccbe6 commit 8d064ef
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ Sentry.init({
tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
});

export const handleError = Sentry.wrapRemixHandleError;
const handleErrorImpl = () => {
Sentry.setTag('remix-test-tag', 'remix-test-value');
};

export const handleError = Sentry.wrapHandleErrorWithSentry(handleErrorImpl);

export default function handleRequest(
request: Request,
Expand Down
7 changes: 6 additions & 1 deletion packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export {
// Keeping the `*` exports for backwards compatibility and types
export * from '@sentry/node';

export { captureRemixServerException, wrapRemixHandleError } from './utils/instrumentServer';
export {
captureRemixServerException,
wrapRemixHandleError,
sentryHandleError,
wrapHandleErrorWithSentry,
} from './utils/instrumentServer';
export { ErrorBoundary, withErrorBoundary } from '@sentry/react';
export { remixRouterInstrumentation, withSentry } from './client/performance';
export { captureRemixErrorBoundaryError } from './client/errors';
Expand Down
24 changes: 22 additions & 2 deletions packages/remix/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ async function extractResponseError(response: Response): Promise<unknown> {
*
* Should be used in `entry.server` like:
*
* export const handleError = Sentry.wrapRemixHandleError
* export const handleError = Sentry.sentryHandleError
*/
export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs): void {
export function sentryHandleError(err: unknown, { request }: DataFunctionArgs): void {
// We are skipping thrown responses here as they are handled by
// `captureRemixServerException` at loader / action level
// We don't want to capture them twice.
Expand All @@ -105,6 +105,26 @@ export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs
});
}

// To be deprecated in favor of `sentryHandleError`
export const wrapRemixHandleError = sentryHandleError;

/**
* Sentry wrapper for Remix's `handleError` function.
* Remix Docs: https://remix.run/docs/en/main/file-conventions/entry.server#handleerror
*/
export function wrapHandleErrorWithSentry(
origHandleError: (err: unknown, args: unknown) => void,
): (err: unknown, args: unknown) => void {
return function (this: unknown, err: unknown, args: unknown): void {
// This is expected to be void but just in case it changes in the future.
const res = origHandleError.call(this, err, args);

sentryHandleError(err, args as DataFunctionArgs);

return res;
};
}

/**
* Captures an exception happened in the Remix server.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/src/utils/vendor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type RemixRequestState = {

export type RemixRequest = Request &
Record<symbol | string, RemixRequestState> & {
agent: Agent | ((parsedURL: URL) => Agent) | undefined;
agent?: Agent | ((parsedURL: URL) => Agent) | undefined;
};

export type AppLoadContext = Record<string, unknown> & { __sentry_express_wrapped__?: boolean };
Expand Down
6 changes: 5 additions & 1 deletion packages/remix/test/integration/app_v2/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Sentry.init({
autoSessionTracking: false,
});

export const handleError = Sentry.wrapRemixHandleError;
const handleErrorImpl = () => {
Sentry.setTag('remix-test-tag', 'remix-test-value');
};

export const handleError = Sentry.wrapHandleErrorWithSentry(handleErrorImpl);

export default function handleRequest(
request: Request,
Expand Down
6 changes: 6 additions & 0 deletions packages/remix/test/integration/test/server/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ describe('Server Side Rendering', () => {
},
},
},
tags: useV2
? {
// Testing that the wrapped `handleError` correctly adds tags
'remix-test-tag': 'remix-test-value',
}
: {},
});

assertSentryEvent(event[2], {
Expand Down

0 comments on commit 8d064ef

Please sign in to comment.