-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(hono): Do not capture 3xx and 4xx errors and add tests #20640
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
6 commits
Select commit
Hold shift + click to select a range
edbc94a
fix(hono): Do not capture 3xx and 4xx errors and add tests
s1gr1d d5b31cc
remove duplicate tests
s1gr1d ae28157
move error tests from route-patterns to errors
s1gr1d 1dfe17a
Merge branch 'develop' into sig/error-tests-hono
s1gr1d cfc56d0
fix bun and node tests
s1gr1d c522c31
fix setting span status
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
45 changes: 45 additions & 0 deletions
45
dev-packages/e2e-tests/test-applications/hono-4/src/route-groups/test-errors.ts
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,45 @@ | ||
| import { Hono } from 'hono'; | ||
| import { HTTPException } from 'hono/http-exception'; | ||
|
|
||
| const errorRoutes = new Hono(); | ||
|
|
||
| // Middleware that throws a 5xx HTTPException (should be captured) | ||
| errorRoutes.use('/middleware-http-exception/*', async (_c, _next) => { | ||
| throw new HTTPException(503, { message: 'Service Unavailable from middleware' }); | ||
| }); | ||
|
|
||
| errorRoutes.get('/middleware-http-exception', c => c.text('should not reach')); | ||
|
|
||
| // Middleware that throws a 4xx HTTPException (should NOT be captured) | ||
| errorRoutes.use('/middleware-http-exception-4xx/*', async (_c, _next) => { | ||
| throw new HTTPException(401, { message: 'Unauthorized from middleware' }); | ||
| }); | ||
|
|
||
| errorRoutes.get('/middleware-http-exception-4xx', c => c.text('should not reach')); | ||
|
|
||
| // Sub-app with a custom onError handler that swallows errors | ||
| const subAppWithOnError = new Hono(); | ||
|
|
||
| subAppWithOnError.onError((err, c) => { | ||
| return c.text(`Handled by onError: ${err.message}`, 500); | ||
| }); | ||
|
|
||
| subAppWithOnError.get('/fail', () => { | ||
| throw new Error('Error caught by custom onError'); | ||
| }); | ||
|
|
||
| errorRoutes.route('/custom-on-error', subAppWithOnError); | ||
|
|
||
| // Nested sub-apps: parent mounts child, child route throws | ||
| const childApp = new Hono(); | ||
|
|
||
| childApp.get('/error', () => { | ||
| throw new Error('Nested child app error'); | ||
| }); | ||
|
|
||
| const parentApp = new Hono(); | ||
| parentApp.route('/child', childApp); | ||
|
|
||
| errorRoutes.route('/nested', parentApp); | ||
|
|
||
| export { errorRoutes }; |
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
263 changes: 230 additions & 33 deletions
263
dev-packages/e2e-tests/test-applications/hono-4/tests/errors.test.ts
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 |
|---|---|---|
| @@ -1,53 +1,250 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError } from '@sentry-internal/test-utils'; | ||
| import { APP_NAME } from './constants'; | ||
| import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; | ||
| import { APP_NAME, RUNTIME } from './constants'; | ||
|
|
||
| test('captures error thrown in route handler', async ({ baseURL }) => { | ||
| const errorWaiter = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'This is a test error for Sentry!'; | ||
| }); | ||
| test.describe('route handler errors', () => { | ||
| test('captures error with mechanism and trace correlation', async ({ baseURL }) => { | ||
| const errorPromise = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'This is a test error for Sentry!'; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return event.contexts?.trace?.op === 'http.server' && !!event.transaction?.includes('/error/'); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/error/test-cause`); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const errorEvent = await errorPromise; | ||
| const transactionEvent = await transactionPromise; | ||
|
|
||
| expect(transactionEvent.transaction).toBe('GET /error/:cause'); | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
|
|
||
| const exception = errorEvent.exception?.values?.[0]; | ||
| expect(exception?.value).toBe('This is a test error for Sentry!'); | ||
| expect(exception?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.http.hono.context_error', | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/error/test-cause`); | ||
| expect(response.status).toBe(500); | ||
| expect(errorEvent.transaction).toBe('GET /error/:cause'); | ||
| expect(errorEvent.request?.method).toBe('GET'); | ||
| expect(errorEvent.request?.url).toContain('/error/test-cause'); | ||
|
|
||
| const event = await errorWaiter; | ||
| expect(event.exception?.values?.[0]?.value).toBe('This is a test error for Sentry!'); | ||
| expect(errorEvent.contexts?.trace?.trace_id).toBe(transactionEvent.contexts?.trace?.trace_id); | ||
| }); | ||
| }); | ||
|
|
||
| test('captures HTTPException with 502 status', async ({ baseURL }) => { | ||
| const errorWaiter = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'HTTPException 502'; | ||
| test.describe('HTTPException errors', () => { | ||
| test('captures 5xx HTTPException', async ({ baseURL }) => { | ||
| const errorPromise = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'HTTPException 500'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/500`); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const errorEvent = await errorPromise; | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('HTTPException 500'); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.http.hono.context_error', | ||
| }); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/502`); | ||
| expect(response.status).toBe(502); | ||
| // On Node/Bun, httpServerSpansIntegration drops transactions for 3xx/4xx responses (ignoreStatusCodes), so we just use a request guard. | ||
| // On Cloudflare the transaction is available, and we additionally verify its name. | ||
| [301, 302].forEach(code => { | ||
| test(`does not capture ${code} HTTPException`, async ({ baseURL }) => { | ||
| let errorEventOccurred = false; | ||
|
|
||
| waitForError(APP_NAME, event => { | ||
| if (event.exception?.values?.[0]?.value === `HTTPException ${code}`) { | ||
| errorEventOccurred = true; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return RUNTIME === 'cloudflare' | ||
| ? event.contexts?.trace?.op === 'http.server' && !!event.transaction?.includes('/http-exception/') | ||
| : event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/${code}`, { redirect: 'manual' }); | ||
| expect(response.status).toBe(code); | ||
|
|
||
| if (RUNTIME !== 'cloudflare') { | ||
| // Simple request guard for non-Cloudflare runtimes since the other transaction is dropped for 4xx responses | ||
| await fetch(`${baseURL}/`); | ||
| } | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| if (RUNTIME === 'cloudflare') { | ||
| expect(transaction.transaction).toBe('GET /http-exception/:code'); | ||
| } | ||
|
|
||
| expect(errorEventOccurred).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| [401, 403, 404].forEach(code => { | ||
| test(`does not capture ${code} HTTPException`, async ({ baseURL }) => { | ||
| let errorEventOccurred = false; | ||
|
|
||
| waitForError(APP_NAME, event => { | ||
| if (event.exception?.values?.[0]?.value === `HTTPException ${code}`) { | ||
| errorEventOccurred = true; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return RUNTIME === 'cloudflare' | ||
| ? event.contexts?.trace?.op === 'http.server' && !!event.transaction?.includes('/http-exception/') | ||
| : event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /'; | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/${code}`); | ||
| expect(response.status).toBe(code); | ||
|
|
||
| if (RUNTIME !== 'cloudflare') { | ||
| // Simple request guard for non-Cloudflare runtimes since the other transaction is dropped for 4xx responses | ||
| await fetch(`${baseURL}/`); | ||
| } | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| if (RUNTIME === 'cloudflare') { | ||
| expect(transaction.transaction).toBe('GET /http-exception/:code'); | ||
| } | ||
|
|
||
| const event = await errorWaiter; | ||
| expect(event.exception?.values?.[0]?.value).toBe('HTTPException 502'); | ||
| expect(errorEventOccurred).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // TODO: 401 and 404 HTTPExceptions should not be captured by Sentry by default, | ||
| // but currently they are. Fix the filtering and update these tests accordingly. | ||
| test('captures HTTPException with 401 status', async ({ baseURL }) => { | ||
| const errorWaiter = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'HTTPException 401'; | ||
| test.describe('middleware errors', () => { | ||
| test('captures 5xx HTTPException thrown in middleware with error span status', async ({ baseURL }) => { | ||
| const errorPromise = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'Service Unavailable from middleware'; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return ( | ||
| event.contexts?.trace?.op === 'http.server' && | ||
| !!event.transaction?.includes('/test-errors/middleware-http-exception') | ||
| ); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/test-errors/middleware-http-exception`); | ||
| expect(response.status).toBe(503); | ||
|
|
||
| const errorEvent = await errorPromise; | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Service Unavailable from middleware'); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism?.type).toBe('auto.middleware.hono'); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism?.handled).toBe(false); | ||
|
|
||
| const transaction = await transactionPromise; | ||
| const middlewareSpan = (transaction.spans || []).find(s => s.op === 'middleware.hono'); | ||
| expect(middlewareSpan?.status).toBe('internal_error'); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/401`); | ||
| expect(response.status).toBe(401); | ||
| test('does not capture 4xx HTTPException thrown in middleware', async ({ baseURL }) => { | ||
| let errorEventOccurred = false; | ||
|
|
||
| waitForError(APP_NAME, event => { | ||
| if (event.exception?.values?.[0]?.value === 'Unauthorized from middleware') { | ||
| errorEventOccurred = true; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| if (RUNTIME === 'cloudflare') { | ||
| return ( | ||
| event.contexts?.trace?.op === 'http.server' && | ||
| !!event.transaction?.includes('/test-errors/middleware-http-exception-4xx') | ||
| ); | ||
| } | ||
| return event.contexts?.trace?.op === 'http.server' && event.transaction === 'GET /'; | ||
| }); | ||
|
|
||
| const event = await errorWaiter; | ||
| expect(event.exception?.values?.[0]?.value).toBe('HTTPException 401'); | ||
| const response = await fetch(`${baseURL}/test-errors/middleware-http-exception-4xx`); | ||
| expect(response.status).toBe(401); | ||
|
|
||
| if (RUNTIME !== 'cloudflare') { | ||
| await fetch(`${baseURL}/`); | ||
| } | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| if (RUNTIME === 'cloudflare') { | ||
| expect(transaction.transaction).toBe('GET /test-errors/middleware-http-exception-4xx/*'); | ||
|
|
||
| const middlewareSpan = (transaction.spans || []).find(s => s.op === 'middleware.hono'); | ||
| expect(middlewareSpan?.status).not.toBe('internal_error'); | ||
| } | ||
|
|
||
| expect(errorEventOccurred).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| test('captures HTTPException with 404 status', async ({ baseURL }) => { | ||
| const errorWaiter = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'HTTPException 404'; | ||
| test.describe('nested sub-app errors', () => { | ||
| test('captures error from nested child sub-app', async ({ baseURL }) => { | ||
| const errorPromise = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'Nested child app error'; | ||
| }); | ||
|
|
||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return event.contexts?.trace?.op === 'http.server' && !!event.transaction?.includes('/nested/child/error'); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/test-errors/nested/child/error`); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const errorEvent = await errorPromise; | ||
| const transaction = await transactionPromise; | ||
|
|
||
| expect(transaction.transaction).toBe('GET /test-errors/nested/child/error'); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Nested child app error'); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.http.hono.context_error', | ||
| }); | ||
| expect(errorEvent.request?.url).toContain('/test-errors/nested/child/error'); | ||
| }); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/http-exception/404`); | ||
| expect(response.status).toBe(404); | ||
| test.describe('custom onError handler', () => { | ||
| test('captures error even when onError handles the response', async ({ baseURL }) => { | ||
| const errorPromise = waitForError(APP_NAME, event => { | ||
| return event.exception?.values?.[0]?.value === 'Error caught by custom onError'; | ||
| }); | ||
|
|
||
| const event = await errorWaiter; | ||
| expect(event.exception?.values?.[0]?.value).toBe('HTTPException 404'); | ||
| const transactionPromise = waitForTransaction(APP_NAME, event => { | ||
| return event.contexts?.trace?.op === 'http.server' && !!event.transaction?.includes('/custom-on-error/fail'); | ||
| }); | ||
|
|
||
| const response = await fetch(`${baseURL}/test-errors/custom-on-error/fail`); | ||
| expect(response.status).toBe(500); | ||
|
|
||
| const body = await response.text(); | ||
| expect(body).toContain('Handled by onError'); | ||
|
|
||
| const errorEvent = await errorPromise; | ||
| const transaction = await transactionPromise; | ||
|
|
||
| expect(transaction.transaction).toBe('GET /test-errors/custom-on-error/fail'); | ||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Error caught by custom onError'); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.http.hono.context_error', | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Not a 100% sure on this one, but according to our develop docs, it's not handled as this was captured by a global error handler (
onError).https://develop.sentry.dev/sdk/telemetry/errors/#concepts
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, should be unhandled 👍