Skip to content

Commit 9e73ddf

Browse files
committed
feat(remix): Add wrapHandleErrorWithSentry
1 parent 2879478 commit 9e73ddf

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
@@ -24,7 +24,11 @@ Sentry.init({
2424
tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
2525
});
2626

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

2933
export default function handleRequest(
3034
request: Request,

packages/remix/src/index.server.ts

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

77-
export { captureRemixServerException, wrapRemixHandleError } from './utils/instrumentServer';
77+
export {
78+
captureRemixServerException,
79+
wrapRemixHandleError,
80+
sentryHandleError,
81+
wrapHandleErrorWithSentry,
82+
} from './utils/instrumentServer';
7883
export { ErrorBoundary, withErrorBoundary } from '@sentry/react';
7984
export { remixRouterInstrumentation, withSentry } from './client/performance';
8085
export { captureRemixErrorBoundaryError } from './client/errors';

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)