diff --git a/packages/hono/src/bun/sdk.ts b/packages/hono/src/bun/sdk.ts index c62dae003f27..26f9c987348b 100644 --- a/packages/hono/src/bun/sdk.ts +++ b/packages/hono/src/bun/sdk.ts @@ -13,13 +13,16 @@ import { LOW_QUALITY_TRANSACTION_PATTERNS } from '../shared/lowQualityTransactio * When manually calling `init`, add the `honoIntegration` to the `integrations` array to set up the Hono integration. */ export function init(options: HonoBunOptions): Client | undefined { - if (getClient()) { + const existingClient = getClient(); + if (existingClient) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( '[Sentry] Sentry is already initialized. Sentry should only be initialized once, through the `sentry()` middleware. Remove the `Sentry.init()` call, if one exists.', ); }); + // Re-initializing would replace the client and drop anything buffered on it + return existingClient; } applySdkMetadata(options, 'hono', ['hono', 'bun']); diff --git a/packages/hono/src/deno/sdk.ts b/packages/hono/src/deno/sdk.ts index 43083981a3c3..1c40bdb3bdbf 100644 --- a/packages/hono/src/deno/sdk.ts +++ b/packages/hono/src/deno/sdk.ts @@ -13,13 +13,16 @@ import { LOW_QUALITY_TRANSACTION_PATTERNS } from '../shared/lowQualityTransactio * When manually calling `init`, add the `honoIntegration` to the `integrations` array to set up the Hono integration. */ export function init(options: HonoDenoOptions): Client | undefined { - if (getClient()) { + const existingClient = getClient(); + if (existingClient) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( '[Sentry] Sentry is already initialized. Sentry should only be initialized once, through the `sentry()` middleware. Remove the `Sentry.init()` call, if one exists.', ); }); + // Re-initializing would replace the client and drop anything buffered on it + return existingClient; } applySdkMetadata(options, 'hono', ['hono', 'deno']); diff --git a/packages/hono/test/bun/middleware.test.ts b/packages/hono/test/bun/middleware.test.ts index 50f70a77883e..f2ef49fa19a8 100644 --- a/packages/hono/test/bun/middleware.test.ts +++ b/packages/hono/test/bun/middleware.test.ts @@ -171,14 +171,22 @@ describe('Hono Bun Middleware', () => { }); describe('double-init guard', () => { - it('still calls init even when Sentry is already initialized', () => { + it('does not re-initialize when Sentry is already initialized', () => { const fakeClient = { getOptions: () => ({}) }; getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); const app = new Hono(); sentry(app, { dsn: 'https://public@dsn.ingest.sentry.io/1337' }); - expect(initBunMock).toHaveBeenCalledTimes(1); + expect(initBunMock).not.toHaveBeenCalled(); + }); + + it('returns the existing client when Sentry is already initialized', () => { + const fakeClient = { getOptions: () => ({}) }; + getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); + + expect(init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' })).toBe(fakeClient); + expect(initBunMock).not.toHaveBeenCalled(); }); it('emits a console.warn directing to remove the duplicate init call when Sentry is already initialized', () => { diff --git a/packages/hono/test/deno/middleware.test.ts b/packages/hono/test/deno/middleware.test.ts index a6caa2d44455..84506ab494fa 100644 --- a/packages/hono/test/deno/middleware.test.ts +++ b/packages/hono/test/deno/middleware.test.ts @@ -171,14 +171,22 @@ describe('Hono Deno Middleware', () => { }); describe('double-init guard', () => { - it('still calls init even when Sentry is already initialized', () => { + it('does not re-initialize when Sentry is already initialized', () => { const fakeClient = { getOptions: () => ({}) }; getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); const app = new Hono(); sentry(app, { dsn: 'https://public@dsn.ingest.sentry.io/1337' }); - expect(initDenoMock).toHaveBeenCalledTimes(1); + expect(initDenoMock).not.toHaveBeenCalled(); + }); + + it('returns the existing client when Sentry is already initialized', () => { + const fakeClient = { getOptions: () => ({}) }; + getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); + + expect(init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' })).toBe(fakeClient); + expect(initDenoMock).not.toHaveBeenCalled(); }); it('emits a console.warn directing to remove the duplicate init call when Sentry is already initialized', () => {