Skip to content

Commit 171aad5

Browse files
committed
feat(cloudflare): Add option to opt out of capturing errors in wrapRequesHandler
1 parent b746c23 commit 171aad5

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

packages/cloudflare/src/request.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ interface RequestHandlerWrapperOptions {
1818
options: CloudflareOptions;
1919
request: Request<unknown, IncomingRequestCfProperties<unknown>>;
2020
context: ExecutionContext;
21+
/**
22+
* If true, errors will be captured, rethrown and sent to Sentry.
23+
* Otherwise, errors are rethrown but not captured.
24+
* @default true
25+
*/
26+
captureErrors?: boolean;
2127
}
2228

2329
/**
@@ -28,7 +34,7 @@ export function wrapRequestHandler(
2834
handler: (...args: unknown[]) => Response | Promise<Response>,
2935
): Promise<Response> {
3036
return withIsolationScope(async isolationScope => {
31-
const { options, request } = wrapperOptions;
37+
const { options, request, captureErrors = true } = wrapperOptions;
3238

3339
// In certain situations, the passed context can become undefined.
3440
// For example, for Astro while prerendering pages at build time.
@@ -67,7 +73,9 @@ export function wrapRequestHandler(
6773
try {
6874
return await handler();
6975
} catch (e) {
70-
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
76+
if (captureErrors) {
77+
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
78+
}
7179
throw e;
7280
} finally {
7381
waitUntil?.(flush(2000));
@@ -91,7 +99,9 @@ export function wrapRequestHandler(
9199
setHttpStatus(span, res.status);
92100
return res;
93101
} catch (e) {
94-
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
102+
if (captureErrors) {
103+
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
104+
}
95105
throw e;
96106
} finally {
97107
waitUntil?.(flush(2000));

packages/cloudflare/test/request.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,33 @@ describe('withSentry', () => {
212212

213213
expect(thrownError).toBe(error);
214214
});
215+
216+
test("doesn't capture errors if `captureErrors` is false", async () => {
217+
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException');
218+
const error = new Error('test');
219+
220+
expect(captureExceptionSpy).not.toHaveBeenCalled();
221+
let thrownError: Error | undefined;
222+
223+
try {
224+
await wrapRequestHandler(
225+
{
226+
options: MOCK_OPTIONS,
227+
request: new Request('https://example.com'),
228+
context: createMockExecutionContext(),
229+
captureErrors: false,
230+
},
231+
() => {
232+
throw error;
233+
},
234+
);
235+
} catch (e: any) {
236+
thrownError = e;
237+
}
238+
239+
expect(captureExceptionSpy).not.toHaveBeenCalled();
240+
expect(thrownError).toBe(error);
241+
});
215242
});
216243

217244
describe('tracing instrumentation', () => {

0 commit comments

Comments
 (0)