@@ -51,13 +51,13 @@ function wrapQuery (original, args, agent, params) {
5151 } else if ( continuationMethod === 'readStream' ) {
5252 return wrapReadStream . call ( this , original , args , reporter )
5353 } else if ( continuationMethod === 'callback' ) { // uses callback
54- return wrapCallback . call ( this , original , _params , args , reporter )
54+ return wrapCallback . call ( this , original , args , reporter )
5555 } else {
5656 return original . apply ( this , args ) // we might not want to instrument the method
5757 }
5858}
5959
60- function wrapCallback ( original , params , args , reporter ) {
60+ function wrapCallback ( original , args , reporter ) {
6161 var wrappedCallback = function ( original ) {
6262 return function ( err ) {
6363 reporter . reportReceive ( err )
@@ -78,33 +78,43 @@ function wrapCallback (original, params, args, reporter) {
7878}
7979
8080function wrapPromise ( original , args , reporter ) {
81- reporter . reportSend ( )
8281 var originalPromise = original . apply ( this , args )
83- return originalPromise . then (
84- function ( v ) { reporter . reportReceive ( ) ; return v } ,
85- function ( err ) { reporter . reportReceive ( err ) ; throw err }
86- )
82+ if ( originalPromise ) {
83+ reporter . reportSend ( )
84+ return originalPromise . then (
85+ function ( v ) {
86+ reporter . reportReceive ( )
87+ return v
88+ } ,
89+ function ( err ) {
90+ reporter . reportReceive ( err )
91+ throw err
92+ }
93+ )
94+ }
8795}
8896
8997function wrapReadStream ( original , args , reporter ) {
90- reporter . reportSend ( )
9198 var originalStream = original . apply ( this , args )
9299
93- originalStream . on ( 'end' , function ( ) {
94- reporter . reportReceive ( )
95- } )
100+ if ( originalStream ) {
101+ reporter . reportSend ( )
102+ originalStream . on ( 'end' , function ( ) {
103+ reporter . reportReceive ( )
104+ } )
96105
97- originalStream . on ( 'error' , function ( err ) {
98- reporter . reportReceive ( err )
106+ originalStream . on ( 'error' , function ( err ) {
107+ reporter . reportReceive ( err )
99108
100- if ( typeof originalStream . listenerCount === 'function' ) {
101- if ( originalStream . listenerCount ( 'error' ) < 2 ) {
109+ if ( typeof originalStream . listenerCount === 'function' ) {
110+ if ( originalStream . listenerCount ( 'error' ) < 2 ) {
111+ throw err
112+ }
113+ } else if ( EventEmitter . listenerCount ( originalStream , 'error' ) < 2 ) {
102114 throw err
103115 }
104- } else if ( EventEmitter . listenerCount ( originalStream , 'error' ) < 2 ) {
105- throw err
106- }
107- } )
116+ } )
117+ }
108118
109119 return originalStream
110120}
0 commit comments