-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(cloudflare): Capture exceptions thrown in hono #16355
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,17 @@ import * as SentryCore from '@sentry/core'; | |
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { CloudflareClient } from '../src/client'; | ||
import { withSentry } from '../src/handler'; | ||
import { markAsInstrumented } from '../src/instrument'; | ||
|
||
// Custom type for hono-like apps (cloudflare handlers) that include errorHandler and onError | ||
type HonoLikeApp<Env = unknown, QueueHandlerMessage = unknown, CfHostMetadata = unknown> = ExportedHandler< | ||
Env, | ||
QueueHandlerMessage, | ||
CfHostMetadata | ||
> & { | ||
onError?: () => void; | ||
errorHandler?: (err: Error) => Response; | ||
}; | ||
|
||
const MOCK_ENV = { | ||
SENTRY_DSN: 'https://[email protected]/1337', | ||
|
@@ -931,6 +942,86 @@ describe('withSentry', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('hono errorHandler', () => { | ||
test('captures errors handled by the errorHandler', async () => { | ||
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException'); | ||
const error = new Error('test hono error'); | ||
|
||
const honoApp = { | ||
fetch(_request, _env, _context) { | ||
return new Response('test'); | ||
}, | ||
onError() {}, // hono-like onError | ||
errorHandler(err: Error) { | ||
return new Response(`Error: ${err.message}`, { status: 500 }); | ||
}, | ||
} satisfies HonoLikeApp<typeof MOCK_ENV>; | ||
|
||
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp); | ||
|
||
// simulates hono's error handling | ||
const errorHandlerResponse = honoApp.errorHandler?.(error); | ||
|
||
expect(captureExceptionSpy).toHaveBeenCalledTimes(1); | ||
expect(captureExceptionSpy).toHaveBeenLastCalledWith(error, { | ||
mechanism: { handled: false, type: 'cloudflare' }, | ||
}); | ||
expect(errorHandlerResponse?.status).toBe(500); | ||
}); | ||
|
||
test('preserves the original errorHandler functionality', async () => { | ||
const originalErrorHandlerSpy = vi.fn().mockImplementation((err: Error) => { | ||
return new Response(`Error: ${err.message}`, { status: 500 }); | ||
}); | ||
|
||
const error = new Error('test hono error'); | ||
|
||
const honoApp = { | ||
fetch(_request, _env, _context) { | ||
return new Response('test'); | ||
}, | ||
onError() {}, // hono-like onError | ||
errorHandler: originalErrorHandlerSpy, | ||
} satisfies HonoLikeApp<typeof MOCK_ENV>; | ||
|
||
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp); | ||
|
||
// Call the errorHandler directly to simulate Hono's error handling | ||
const errorHandlerResponse = honoApp.errorHandler?.(error); | ||
|
||
expect(originalErrorHandlerSpy).toHaveBeenCalledTimes(1); | ||
expect(originalErrorHandlerSpy).toHaveBeenLastCalledWith(error); | ||
expect(errorHandlerResponse?.status).toBe(500); | ||
}); | ||
|
||
test('does not instrument an already instrumented errorHandler', async () => { | ||
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException'); | ||
const error = new Error('test hono error'); | ||
|
||
// Create a handler with an errorHandler that's already been instrumented | ||
const originalErrorHandler = (err: Error) => { | ||
return new Response(`Error: ${err.message}`, { status: 500 }); | ||
}; | ||
|
||
// Mark as instrumented before wrapping | ||
markAsInstrumented(originalErrorHandler); | ||
|
||
const honoApp = { | ||
fetch(_request, _env, _context) { | ||
return new Response('test'); | ||
}, | ||
onError() {}, // hono-like onError | ||
errorHandler: originalErrorHandler, | ||
} satisfies HonoLikeApp<typeof MOCK_ENV>; | ||
|
||
withSentry(env => ({ dsn: env.SENTRY_DSN }), honoApp); | ||
|
||
// The errorHandler should not have been wrapped again | ||
honoApp.errorHandler?.(error); | ||
expect(captureExceptionSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
function createMockExecutionContext(): ExecutionContext { | ||
|
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.
So this is always defined right? We don't need to create one for a user if it's not defined?
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.
yes, hono has a default error handler: https://github.com/honojs/hono/blob/bb7afaccfd5b6b514da356e069f27eb5ccfc0e3b/src/hono-base.ts#L36