-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13cbc55
add Hono integration
s1gr1d 26628c7
add tests
s1gr1d a9d5e79
add changelog entry
s1gr1d 28789bb
remove code in setupOnce
s1gr1d 07d8e67
add __DEBUG_BUILD__ mock
s1gr1d 05fdea1
add vite config
s1gr1d e910c66
review comments
s1gr1d 40c50df
fix lint error
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://[email protected]/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(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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. |
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
errorHandler
proxy 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.