Skip to content
Draft
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
13 changes: 13 additions & 0 deletions packages/cloudflare/src/baseSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ export function getDefaultIntegrations(options: CloudflareOptions): Integration[
* disposal would otherwise be lost.
*/
export function initBaseSdk(options: CloudflareOptions): CloudflareClient | undefined {
// The wrappers call this inside the user's handlers and Durable Object constructors, so a setup
// that throws (e.g. in a custom transport or integration) must not fail them.
try {
return setupClient(options);
} catch (error) {
DEBUG_BUILD && debug.error('[Sentry] Failed to initialize the SDK, continuing without Sentry:', error);
// A client whose setup did not complete can miss integrations, so it must not send events.
getCurrentScope().setClient(undefined);
return undefined;
}
}

function setupClient(options: CloudflareOptions): CloudflareClient {
const cached = getCachedClient();
// oxlint-disable-next-line typescript/no-deprecated
const cacheEnabled = options.cacheClient !== false;
Expand Down
30 changes: 30 additions & 0 deletions packages/cloudflare/test/durableobject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,36 @@ describe('instrumentDurableObjectWithSentry', () => {
expect(constructorClient !== undefined).toBe(hasClient);
});

it('constructs and runs handlers without Sentry when the SDK setup throws', async () => {
let alarmClient: unknown = 'alarm did not run';
const testClass = class {
alarm() {
alarmClient = SentryCore.getClient();
}
};
const obj = Reflect.construct(
instrumentDurableObjectWithSentry(
() => ({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [
{
name: 'ThrowingIntegration',
setup() {
throw new Error('setup failed');
},
},
],
}),
testClass as any,
),
[{ waitUntil: vi.fn() }, {}],
);

await obj.alarm();

expect(alarmClient).toBeUndefined();
});

it('does not apply scope data set in the constructor to later invocations', async () => {
const events: Event[] = [];
const waits: Promise<unknown>[] = [];
Expand Down
Loading