Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Add honoIntegration with error-filtering function#17743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
13cbc5526628c7a9d5e7928789bb07d8e6705fdea1e910c6640c50dfFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
| import { captureException, debug, defineIntegration, getClient } from '@sentry/core'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| const INTEGRATION_NAME = 'Hono'; | ||
| interface HonoError extends Error { | ||
| status?: number; | ||
| } | ||
| export interface Options { | ||
| /** | ||
| * Callback method deciding whether error should be captured and sent to Sentry | ||
| * @param error Captured middleware error | ||
| */ | ||
| shouldHandleError?(this: void, error: HonoError): boolean; | ||
| } | ||
| /** Only exported for internal use */ | ||
| export function getHonoIntegration(): ReturnType<typeof _honoIntegration> | undefined { | ||
| return getClient()?.getIntegrationByName(INTEGRATION_NAME); | ||
| } | ||
| function isHonoError(err: unknown): err is HonoError { | ||
| if (err instanceof Error) { | ||
| return true; | ||
| } | ||
| return typeof err === 'object' && err !== null && 'status' in (err as Record<string, unknown>); | ||
| } | ||
| const _honoIntegration = ((options: Partial<Options> = {}) => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| handleHonoException(err: HonoError): void { | ||
| const shouldHandleError = options.shouldHandleError || defaultShouldHandleError; | ||
| if (!isHonoError(err)) { | ||
| DEBUG_BUILD && debug.log("[Hono] Won't capture exception in `onError` because it's not a Hono error.", err); | ||
| return; | ||
| } | ||
| if (shouldHandleError(err)) { | ||
| captureException(err, { mechanism: { handled: false, type: 'auto.faas.hono.error_handler' } }); | ||
| } else { | ||
| DEBUG_BUILD && debug.log('[Hono] Not capturing exception because `shouldHandleError` returned `false`.', err); | ||
| } | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
| /** | ||
| * Automatically captures exceptions caught with the `onError` handler in Hono. | ||
| * | ||
| * The integration is enabled by default. | ||
| * | ||
| * @example | ||
| * integrations: [ | ||
| * honoIntegration({ | ||
| * shouldHandleError: (err) => true; // always capture exceptions in onError | ||
| * }) | ||
| * ] | ||
| */ | ||
| export const honoIntegration = defineIntegration(_honoIntegration); | ||
| /** | ||
| * Default function to determine if an error should be sent to Sentry | ||
| * | ||
| * 3xx and 4xx errors are not sent by default. | ||
| */ | ||
| function defaultShouldHandleError(error: HonoError): boolean { | ||
| const statusCode = error?.status; | ||
| // 3xx and 4xx errors are not sent by default. | ||
| return statusCode ? statusCode >= 500 || statusCode <= 299 : true; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import * as sentryCore from '@sentry/core'; | ||
| import { type Client, createStackParser } from '@sentry/core'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { CloudflareClient } from '../../src/client'; | ||
| import { honoIntegration } from '../../src/integrations/hono'; | ||
| class FakeClient extends CloudflareClient { | ||
| public getIntegrationByName(name: string) { | ||
| return name === 'Hono' ? (honoIntegration() as any) : undefined; | ||
| } | ||
| } | ||
| type MockHonoIntegrationType = { handleHonoException: (err: Error) => void }; | ||
| describe('Hono integration', () => { | ||
| let client: FakeClient; | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| client = new FakeClient({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [], | ||
| transport: () => ({ send: () => Promise.resolve({}), flush: () => Promise.resolve(true) }), | ||
| stackParser: createStackParser(), | ||
| }); | ||
| vi.spyOn(sentryCore, 'getClient').mockImplementation(() => client as Client); | ||
| }); | ||
| it('captures in errorHandler when onError exists', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration(); | ||
| integration.setupOnce?.(); | ||
| const error = new Error('hono boom'); | ||
| // simulate withSentry wrapping of errorHandler calling back into integration | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException(error); | ||
| expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
| expect(captureExceptionSpy).toHaveBeenLastCalledWith(error, { | ||
| mechanism: { handled: false, type: 'auto.faas.hono.error_handler' }, | ||
| }); | ||
| }); | ||
| it('does not capture for 4xx status', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration(); | ||
| integration.setupOnce?.(); | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException( | ||
| Object.assign(new Error('client err'), { status: 404 }), | ||
| ); | ||
| expect(captureExceptionSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| it('does not capture for 3xx status', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration(); | ||
| integration.setupOnce?.(); | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException( | ||
| Object.assign(new Error('redirect'), { status: 302 }), | ||
| ); | ||
| expect(captureExceptionSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| it('captures for 5xx status', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration(); | ||
| integration.setupOnce?.(); | ||
| const err = Object.assign(new Error('server err'), { status: 500 }); | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException(err); | ||
| expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| it('captures if no status is present on Error', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration(); | ||
| integration.setupOnce?.(); | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException(new Error('no status')); | ||
| expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| it('supports custom shouldHandleError option', () => { | ||
| const captureExceptionSpy = vi.spyOn(sentryCore, 'captureException'); | ||
| const integration = honoIntegration({ shouldHandleError: () => false }); | ||
| integration.setupOnce?.(); | ||
| (integration as unknown as MockHonoIntegrationType).handleHonoException(new Error('blocked')); | ||
| expect(captureExceptionSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
| import baseConfig from '../../vite/vite.config'; | ||
| export default defineConfig({ | ||
| ...baseConfig, | ||
MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to add a vite.config.ts file because the ...and this was needed to make the unit tests work. | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Error Handling Regression in SDK
The
errorHandlerproxy now usesgetHonoIntegration()?.handleHonoException(err). This change can silently drop exceptions ifgetHonoIntegration()returnsundefined(e.g., SDK not initialized or Hono integration is missing), a regression from the previous guaranteed error capture.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the reason of the PR...
The client is defined at this point. It was the same before. If there was no client, it did not capture.