|
| 1 | +import * as sentryCore from '@sentry/core'; |
| 2 | +import { type Client, createStackParser } from '@sentry/core'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { CloudflareClient } from '../../src/client'; |
| 5 | +import { honoIntegration } from '../../src/integrations/hono'; |
| 6 | + |
| 7 | +class FakeClient extends CloudflareClient { |
| 8 | + public getIntegrationByName(name: string) { |
| 9 | + return name === 'Hono' ? (honoIntegration() as any) : undefined; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +type MockHonoIntegrationType = { handleHonoException: (err: Error) => void }; |
| 14 | + |
| 15 | +describe('Hono integration', () => { |
| 16 | + let client: FakeClient; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | + client = new FakeClient({ |
| 21 | + dsn: 'https://[email protected]/1337', |
| 22 | + integrations: [], |
| 23 | + transport: () => ({ send: () => Promise.resolve({}), flush: () => Promise.resolve(true) }), |
| 24 | + stackParser: createStackParser(), |
| 25 | + }); |
| 26 | + |
| 27 | + vi.spyOn(sentryCore, 'getClient').mockImplementation(() => client as Client); |
| 28 | + }); |
| 29 | + |
| 30 | + it('captures in errorHandler when onError exists', () => { |
| 31 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 32 | + const integration = honoIntegration(); |
| 33 | + integration.setupOnce?.(); |
| 34 | + |
| 35 | + const error = new Error('hono boom'); |
| 36 | + // simulate withSentry wrapping of errorHandler calling back into integration |
| 37 | + (integration as unknown as MockHonoIntegrationType).handleHonoException(error); |
| 38 | + |
| 39 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(1); |
| 40 | + expect(captureExceptionSpy).toHaveBeenLastCalledWith(error, { |
| 41 | + mechanism: { handled: false, type: 'auto.faas.hono.error_handler' }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not capture for 4xx status', () => { |
| 46 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 47 | + const integration = honoIntegration(); |
| 48 | + integration.setupOnce?.(); |
| 49 | + |
| 50 | + (integration as unknown as MockHonoIntegrationType).handleHonoException( |
| 51 | + Object.assign(new Error('client err'), { status: 404 }), |
| 52 | + ); |
| 53 | + expect(captureExceptionSpy).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not capture for 3xx status', () => { |
| 57 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 58 | + const integration = honoIntegration(); |
| 59 | + integration.setupOnce?.(); |
| 60 | + |
| 61 | + (integration as unknown as MockHonoIntegrationType).handleHonoException( |
| 62 | + Object.assign(new Error('redirect'), { status: 302 }), |
| 63 | + ); |
| 64 | + expect(captureExceptionSpy).not.toHaveBeenCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('captures for 5xx status', () => { |
| 68 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 69 | + const integration = honoIntegration(); |
| 70 | + integration.setupOnce?.(); |
| 71 | + |
| 72 | + const err = Object.assign(new Error('server err'), { status: 500 }); |
| 73 | + (integration as unknown as MockHonoIntegrationType).handleHonoException(err); |
| 74 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(1); |
| 75 | + }); |
| 76 | + |
| 77 | + it('captures if no status is present on Error', () => { |
| 78 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 79 | + const integration = honoIntegration(); |
| 80 | + integration.setupOnce?.(); |
| 81 | + |
| 82 | + (integration as unknown as MockHonoIntegrationType).handleHonoException(new Error('no status')); |
| 83 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('supports custom shouldHandleError option', () => { |
| 87 | + const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); |
| 88 | + const integration = honoIntegration({ shouldHandleError: () => false }); |
| 89 | + integration.setupOnce?.(); |
| 90 | + |
| 91 | + (integration as unknown as MockHonoIntegrationType).handleHonoException(new Error('blocked')); |
| 92 | + expect(captureExceptionSpy).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments