@@ -17,10 +17,16 @@ test.beforeEach((ctx) => {
1717 function handler ( arg1 , arg2 , arg3 ) {
1818 return `${ arg1 } , ${ arg2 } , ${ arg3 } `
1919 }
20+ const txInfo = { }
21+ wrapper . extractTxInfo = function ( ) {
22+ const request = { }
23+ return { txInfo, request, errorWare : false }
24+ }
2025 ctx . nr = {
2126 agent,
2227 handler,
2328 logger,
29+ txInfo,
2430 wrapper
2531 }
2632} )
@@ -69,3 +75,62 @@ test('should run wrapped handler in context if transaction present, and properly
6975 end ( )
7076 } )
7177} )
78+
79+ test ( 'should handle error when passed in to done handler' , function ( t , end ) {
80+ const { agent, txInfo, wrapper } = t . nr
81+ const error = new Error ( 'test error' )
82+ function handler ( req , res , next ) {
83+ next ( error )
84+ }
85+ const route = '/test/url'
86+ const wrapped = wrapper . wrap ( { handler, route } )
87+ helper . runInTransaction ( agent , function ( tx ) {
88+ tx . type = 'web'
89+ tx . url = route
90+ wrapped ( 'one' , 'two' , function ( ) { } )
91+ assert . deepEqual ( txInfo . error , error )
92+ assert . equal ( txInfo . errorHandled , false )
93+
94+ tx . end ( )
95+ end ( )
96+ } )
97+ } )
98+
99+ test ( 'should not handle error when no error is passed to done handler' , function ( t , end ) {
100+ const { agent, txInfo, wrapper } = t . nr
101+ function handler ( req , res , next ) {
102+ next ( )
103+ }
104+ const route = '/test/url'
105+ const wrapped = wrapper . wrap ( { handler, route } )
106+ helper . runInTransaction ( agent , function ( tx ) {
107+ tx . type = 'web'
108+ tx . url = route
109+ wrapped ( 'one' , 'two' , function ( ) { } )
110+ assert . deepEqual ( txInfo , { } )
111+
112+ tx . end ( )
113+ end ( )
114+ } )
115+ } )
116+
117+ test ( 'should not handle error when isError is not using default handler' , function ( t , end ) {
118+ const { agent, txInfo, wrapper } = t . nr
119+ const error = new Error ( 'test error' )
120+ function handler ( req , res , next ) {
121+ next ( error )
122+ }
123+ const route = '/test/url'
124+ const wrapped = wrapper . wrap ( { handler, route } )
125+ wrapper . isError = function ( err ) {
126+ return err . message !== 'test error'
127+ }
128+ helper . runInTransaction ( agent , function ( tx ) {
129+ tx . type = 'web'
130+ tx . url = route
131+ wrapped ( 'one' , 'two' , function ( ) { } )
132+ assert . deepEqual ( txInfo , { } )
133+ tx . end ( )
134+ end ( )
135+ } )
136+ } )
0 commit comments