@@ -2,6 +2,7 @@ import * as sentryCore from '@sentry/core';
22import { type Client , createStackParser } from '@sentry/core' ;
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44import { CloudflareClient } from '../../src/client' ;
5+ import type { HonoContext } from '../../src/integrations/hono' ;
56import { honoIntegration } from '../../src/integrations/hono' ;
67
78class FakeClient extends CloudflareClient {
@@ -10,7 +11,11 @@ class FakeClient extends CloudflareClient {
1011 }
1112}
1213
13- type MockHonoIntegrationType = { handleHonoException : ( err : Error ) => void } ;
14+ type MockHonoIntegrationType = { handleHonoException : ( err : Error , ctx : HonoContext ) => void } ;
15+
16+ const sampleContext : HonoContext = {
17+ req : { method : 'GET' , path : '/vitest-sample' } ,
18+ } ;
1419
1520describe ( 'Hono integration' , ( ) => {
1621 let client : FakeClient ;
@@ -34,7 +39,7 @@ describe('Hono integration', () => {
3439
3540 const error = new Error ( 'hono boom' ) ;
3641 // simulate withSentry wrapping of errorHandler calling back into integration
37- ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( error ) ;
42+ ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( error , sampleContext ) ;
3843
3944 expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
4045 expect ( captureExceptionSpy ) . toHaveBeenLastCalledWith ( error , {
@@ -49,6 +54,7 @@ describe('Hono integration', () => {
4954
5055 ( integration as unknown as MockHonoIntegrationType ) . handleHonoException (
5156 Object . assign ( new Error ( 'client err' ) , { status : 404 } ) ,
57+ sampleContext ,
5258 ) ;
5359 expect ( captureExceptionSpy ) . not . toHaveBeenCalled ( ) ;
5460 } ) ;
@@ -60,6 +66,7 @@ describe('Hono integration', () => {
6066
6167 ( integration as unknown as MockHonoIntegrationType ) . handleHonoException (
6268 Object . assign ( new Error ( 'redirect' ) , { status : 302 } ) ,
69+ sampleContext ,
6370 ) ;
6471 expect ( captureExceptionSpy ) . not . toHaveBeenCalled ( ) ;
6572 } ) ;
@@ -70,7 +77,7 @@ describe('Hono integration', () => {
7077 integration . setupOnce ?.( ) ;
7178
7279 const err = Object . assign ( new Error ( 'server err' ) , { status : 500 } ) ;
73- ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( err ) ;
80+ ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( err , sampleContext ) ;
7481 expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
7582 } ) ;
7683
@@ -79,7 +86,7 @@ describe('Hono integration', () => {
7986 const integration = honoIntegration ( ) ;
8087 integration . setupOnce ?.( ) ;
8188
82- ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( new Error ( 'no status' ) ) ;
89+ ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( new Error ( 'no status' ) , sampleContext ) ;
8390 expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
8491 } ) ;
8592
@@ -88,7 +95,17 @@ describe('Hono integration', () => {
8895 const integration = honoIntegration ( { shouldHandleError : ( ) => false } ) ;
8996 integration . setupOnce ?.( ) ;
9097
91- ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( new Error ( 'blocked' ) ) ;
98+ ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( new Error ( 'blocked' ) , sampleContext ) ;
9299 expect ( captureExceptionSpy ) . not . toHaveBeenCalled ( ) ;
93100 } ) ;
101+
102+ it ( 'does not throw error without passed context and still captures' , ( ) => {
103+ const captureExceptionSpy = vi . spyOn ( sentryCore , 'captureException' ) ;
104+ const integration = honoIntegration ( ) ;
105+ integration . setupOnce ?.( ) ;
106+
107+ // @ts -expect-error context is not passed
108+ ( integration as unknown as MockHonoIntegrationType ) . handleHonoException ( new Error ( ) ) ;
109+ expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
110+ } ) ;
94111} ) ;
0 commit comments