Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions packages/cloudflare/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
handler.fetch = new Proxy(handler.fetch, {
apply(target, thisArg, args: Parameters<ExportedHandlerFetchHandler<Env, CfHostMetadata>>) {
const [request, env, context] = args;
const options = optionsCallback(env);
const callbackOptions = optionsCallback(env);

const options = { ...getOptionsFromEnv(env), ...callbackOptions };

return wrapRequestHandler({ options, request, context }, () => target.apply(thisArg, args));
},
});
Expand All @@ -48,7 +51,10 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
apply(target, thisArg, args: Parameters<ExportedHandlerScheduledHandler<Env>>) {
const [event, env, context] = args;
return withIsolationScope(isolationScope => {
const options = optionsCallback(env);
const callbackOptions = optionsCallback(env);

const options = { ...getOptionsFromEnv(env), ...callbackOptions };

const client = init(options);
isolationScope.setClient(client);

Expand Down Expand Up @@ -91,3 +97,13 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM

return handler;
}

function getOptionsFromEnv(env: unknown): CloudflareOptions {
if (typeof env !== 'object' || env === null) {
return {};
}

return {
release: 'SENTRY_RELEASE' in env && typeof env.SENTRY_RELEASE === 'string' ? env.SENTRY_RELEASE : undefined,
};
}
109 changes: 109 additions & 0 deletions packages/cloudflare/test/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { withSentry } from '../src/handler';

const MOCK_ENV = {
SENTRY_DSN: 'https://[email protected]/1337',
SENTRY_RELEASE: '1.1.1',
};

describe('withSentry', () => {
Expand Down Expand Up @@ -51,6 +52,65 @@ describe('withSentry', () => {

expect(result).toBe(response);
});

test('merges options from env and callback', async () => {
const handler = {
fetch(_request, _env, _context) {
throw new Error('test');
},
} satisfies ExportedHandler<typeof MOCK_ENV>;

let sentryEvent: Event = {};

const wrappedHandler = withSentry(
env => ({
dsn: env.SENTRY_DSN,
beforeSend(event) {
sentryEvent = event;
return null;
},
}),
handler,
);

try {
await wrappedHandler.fetch(new Request('https://example.com'), MOCK_ENV, createMockExecutionContext());
} catch {
// ignore
}

expect(sentryEvent.release).toEqual('1.1.1');
});

test('callback options take precedence over env options', async () => {
const handler = {
fetch(_request, _env, _context) {
throw new Error('test');
},
} satisfies ExportedHandler<typeof MOCK_ENV>;

let sentryEvent: Event = {};

const wrappedHandler = withSentry(
env => ({
dsn: env.SENTRY_DSN,
release: '2.0.0',
beforeSend(event) {
sentryEvent = event;
return null;
},
}),
handler,
);

try {
await wrappedHandler.fetch(new Request('https://example.com'), MOCK_ENV, createMockExecutionContext());
} catch {
// ignore
}

expect(sentryEvent.release).toEqual('2.0.0');
});
});

describe('scheduled handler', () => {
Expand All @@ -70,6 +130,55 @@ describe('withSentry', () => {
expect(optionsCallback).toHaveBeenLastCalledWith(MOCK_ENV);
});

test('merges options from env and callback', async () => {
const handler = {
scheduled(_controller, _env, _context) {
SentryCore.captureMessage('cloud_resource');
return;
},
} satisfies ExportedHandler<typeof MOCK_ENV>;

let sentryEvent: Event = {};
const wrappedHandler = withSentry(
env => ({
dsn: env.SENTRY_DSN,
beforeSend(event) {
sentryEvent = event;
return null;
},
}),
handler,
);
await wrappedHandler.scheduled(createMockScheduledController(), MOCK_ENV, createMockExecutionContext());

expect(sentryEvent.release).toBe('1.1.1');
});

test('callback options take precedence over env options', async () => {
const handler = {
scheduled(_controller, _env, _context) {
SentryCore.captureMessage('cloud_resource');
return;
},
} satisfies ExportedHandler<typeof MOCK_ENV>;

let sentryEvent: Event = {};
const wrappedHandler = withSentry(
env => ({
dsn: env.SENTRY_DSN,
release: '2.0.0',
beforeSend(event) {
sentryEvent = event;
return null;
},
}),
handler,
);
await wrappedHandler.scheduled(createMockScheduledController(), MOCK_ENV, createMockExecutionContext());

expect(sentryEvent.release).toEqual('2.0.0');
});

test('flushes the event after the handler is done using the cloudflare context.waitUntil', async () => {
const handler = {
scheduled(_controller, _env, _context) {
Expand Down
Loading