Skip to content

Commit 848af27

Browse files
committed
feat(remix): Add wrapHandleErrorWithSentry
1 parent bee0677 commit 848af27

File tree

6 files changed

+52
-6
lines changed

6 files changed

+52
-6
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ installGlobals();
2626

2727
const ABORT_DELAY = 5_000;
2828

29-
export const handleError = Sentry.wrapRemixHandleError;
29+
Sentry.init({
30+
environment: 'qa', // dynamic sampling bias to keep transactions
31+
dsn: process.env.E2E_TEST_DSN,
32+
// Performance Monitoring
33+
tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
34+
});
35+
36+
const handleErrorImpl = () => {
37+
Sentry.setTag('remix-test-tag', 'remix-test-value');
38+
};
39+
40+
export const handleError = Sentry.wrapHandleErrorWithSentry(handleErrorImpl);
3041

3142
export default function handleRequest(
3243
request: Request,

packages/remix/src/index.server.ts

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

106-
export { captureRemixServerException, wrapRemixHandleError } from './utils/instrumentServer';
106+
export {
107+
captureRemixServerException,
108+
wrapRemixHandleError,
109+
sentryHandleError,
110+
wrapHandleErrorWithSentry,
111+
} from './utils/instrumentServer';
107112
export { ErrorBoundary, withErrorBoundary } from '@sentry/react';
108113
export { withSentry } from './client/performance';
109114
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
@@ -89,9 +89,9 @@ async function extractResponseError(response: Response): Promise<unknown> {
8989
*
9090
* Should be used in `entry.server` like:
9191
*
92-
* export const handleError = Sentry.wrapRemixHandleError
92+
* export const handleError = Sentry.sentryHandleError
9393
*/
94-
export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs): void {
94+
export function sentryHandleError(err: unknown, { request }: DataFunctionArgs): void {
9595
// We are skipping thrown responses here as they are handled by
9696
// `captureRemixServerException` at loader / action level
9797
// We don't want to capture them twice.
@@ -107,6 +107,26 @@ export function wrapRemixHandleError(err: unknown, { request }: DataFunctionArgs
107107
});
108108
}
109109

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

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
@@ -13,7 +13,11 @@ import type { EntryContext } from '@remix-run/node';
1313
import { RemixServer } from '@remix-run/react';
1414
import { renderToString } from 'react-dom/server';
1515

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

1822
export default function handleRequest(
1923
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
@@ -19,6 +19,12 @@ describe('Server Side Rendering', () => {
1919
},
2020
},
2121
},
22+
tags: useV2
23+
? {
24+
// Testing that the wrapped `handleError` correctly adds tags
25+
'remix-test-tag': 'remix-test-value',
26+
}
27+
: {},
2228
});
2329

2430
assertSentryEvent(event[2], {

0 commit comments

Comments
 (0)