Skip to content

Commit 3e96ef4

Browse files
committed
feat(remix): Add wrapHandleErrorWithSentry
1 parent 3b1d836 commit 3e96ef4

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

dev-packages/e2e-tests/test-applications/create-remix-app-v2/app/entry.server.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ Sentry.init({
2525
tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
2626
});
2727

28-
export const handleError = Sentry.wrapRemixHandleError;
28+
const handleErrorImpl = () => {
29+
Sentry.setTag('remix-test-tag', 'remix-test-value');
30+
};
31+
32+
export const handleError = Sentry.wrapHandleErrorWithSentry(handleErrorImpl);
2933

3034
export default function handleRequest(
3135
request: Request,

packages/remix/src/index.server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export {
9999
// Keeping the `*` exports for backwards compatibility and types
100100
export * from '@sentry/node';
101101

102-
export { captureRemixServerException, wrapRemixHandleError } from './utils/instrumentServer';
102+
export {
103+
captureRemixServerException,
104+
wrapRemixHandleError,
105+
sentryHandleError,
106+
wrapHandleErrorWithSentry,
107+
} from './utils/instrumentServer';
103108
export { ErrorBoundary, withErrorBoundary } from '@sentry/react';
104109
// eslint-disable-next-line deprecation/deprecation
105110
export { remixRouterInstrumentation, withSentry } from './client/performance';

packages/remix/src/utils/instrumentServer.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ async function extractResponseError(response: Response): Promise<unknown> {
8787
*
8888
* Should be used in `entry.server` like:
8989
*
90-
* export const handleError = Sentry.wrapRemixHandleError
90+
* export const handleError = Sentry.sentryHandleError
9191
*/
92-
export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs): void {
92+
export function sentryHandleError(err: unknown, { request }: DataFunctionArgs): void {
9393
// We are skipping thrown responses here as they are handled by
9494
// `captureRemixServerException` at loader / action level
9595
// We don't want to capture them twice.
@@ -105,6 +105,26 @@ export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs
105105
});
106106
}
107107

108+
// To be deprecated in favor of `sentryHandleError`
109+
export const wrapRemixHandleError = sentryHandleError;
110+
111+
/**
112+
* Sentry wrapper for Remix's `handleError` function.
113+
* Remix Docs: https://remix.run/docs/en/main/file-conventions/entry.server#handleerror
114+
*/
115+
export function wrapHandleErrorWithSentry(
116+
origHandleError: (err: unknown, args: unknown) => void,
117+
): (err: unknown, args: unknown) => void {
118+
return function (this: unknown, err: unknown, args: unknown): void {
119+
// This is expected to be void but just in case it changes in the future.
120+
const res = origHandleError.call(this, err, args);
121+
122+
sentryHandleError(err, args as DataFunctionArgs);
123+
124+
return res;
125+
};
126+
}
127+
108128
/**
109129
* Captures an exception happened in the Remix server.
110130
*

packages/remix/src/utils/vendor/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export type RemixRequestState = {
6161

6262
export type RemixRequest = Request &
6363
Record<symbol | string, RemixRequestState> & {
64-
agent: Agent | ((parsedURL: URL) => Agent) | undefined;
64+
agent?: Agent | ((parsedURL: URL) => Agent) | undefined;
6565
};
6666

6767
export type AppLoadContext = Record<string, unknown> & { __sentry_express_wrapped__?: boolean };

packages/remix/test/integration/app_v2/entry.server.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Sentry.init({
1111
autoSessionTracking: false,
1212
});
1313

14-
export const handleError = Sentry.wrapRemixHandleError;
14+
const handleErrorImpl = () => {
15+
Sentry.setTag('remix-test-tag', 'remix-test-value');
16+
};
17+
18+
export const handleError = Sentry.wrapHandleErrorWithSentry(handleErrorImpl);
1519

1620
export default function handleRequest(
1721
request: Request,

packages/remix/test/integration/test/server/ssr.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe('Server Side Rendering', () => {
2222
},
2323
},
2424
},
25+
tags: useV2
26+
? {
27+
// Testing that the wrapped `handleError` correctly adds tags
28+
'remix-test-tag': 'remix-test-value',
29+
}
30+
: {},
2531
});
2632

2733
assertSentryEvent(event[2], {

0 commit comments

Comments
 (0)