Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/cloudflare/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ interface RequestHandlerWrapperOptions {
options: CloudflareOptions;
request: Request<unknown, IncomingRequestCfProperties<unknown>>;
context: ExecutionContext;
/**
* If true, errors will be captured, rethrown and sent to Sentry.
* Otherwise, errors are rethrown but not captured.
*
* You most likely don't want to set this to `false`, if you use `wrapRequestHandler` directly.
* This is primarily meant as an escape hatch for higher-level SDKs relying on additional error
* capturing mechanisms where this wrapper captures errors too early or too generally.
*
* @default true
*/
captureErrors?: boolean;
}

/**
Expand All @@ -28,7 +39,7 @@ export function wrapRequestHandler(
handler: (...args: unknown[]) => Response | Promise<Response>,
): Promise<Response> {
return withIsolationScope(async isolationScope => {
const { options, request } = wrapperOptions;
const { options, request, captureErrors = true } = wrapperOptions;

// In certain situations, the passed context can become undefined.
// For example, for Astro while prerendering pages at build time.
Expand Down Expand Up @@ -67,7 +78,9 @@ export function wrapRequestHandler(
try {
return await handler();
} catch (e) {
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
if (captureErrors) {
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
}
throw e;
} finally {
waitUntil?.(flush(2000));
Expand All @@ -91,7 +104,9 @@ export function wrapRequestHandler(
setHttpStatus(span, res.status);
return res;
} catch (e) {
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
if (captureErrors) {
captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
}
throw e;
} finally {
waitUntil?.(flush(2000));
Expand Down
27 changes: 27 additions & 0 deletions packages/cloudflare/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,33 @@ describe('withSentry', () => {

expect(thrownError).toBe(error);
});

test("doesn't capture errors if `captureErrors` is false", async () => {
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException');
const error = new Error('test');

expect(captureExceptionSpy).not.toHaveBeenCalled();
let thrownError: Error | undefined;

try {
await wrapRequestHandler(
{
options: MOCK_OPTIONS,
request: new Request('https://example.com'),
context: createMockExecutionContext(),
captureErrors: false,
},
() => {
throw error;
},
);
} catch (e: any) {
thrownError = e;
}

expect(captureExceptionSpy).not.toHaveBeenCalled();
expect(thrownError).toBe(error);
});
});

describe('tracing instrumentation', () => {
Expand Down
Loading