@@ -58,6 +58,9 @@ export class OnUncaughtException implements Integration {
58
58
return ( error : Error ) : void => {
59
59
let onFatalError : OnFatalErrorHandler = logAndExitProcess ;
60
60
const client = getCurrentHub ( ) . getClient < NodeClient > ( ) ;
61
+ // in order to honour Node's original behavior on uncaught exceptions, we should not
62
+ // exit the process if the app added its own 'uncaughtException' listener
63
+ const shouldExitProcess : boolean = global . process . listenerCount ( 'uncaughtException' ) === 1 ;
61
64
62
65
if ( this . _options . onFatalError ) {
63
66
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -83,23 +86,23 @@ export class OnUncaughtException implements Integration {
83
86
originalException : error ,
84
87
data : { mechanism : { handled : false , type : 'onuncaughtexception' } } ,
85
88
} ) ;
86
- if ( ! calledFatalError ) {
89
+ if ( ! calledFatalError && shouldExitProcess ) {
87
90
calledFatalError = true ;
88
91
onFatalError ( error ) ;
89
92
}
90
93
} ) ;
91
94
} else {
92
- if ( ! calledFatalError ) {
95
+ if ( ! calledFatalError && shouldExitProcess ) {
93
96
calledFatalError = true ;
94
97
onFatalError ( error ) ;
95
98
}
96
99
}
97
- } else if ( calledFatalError ) {
100
+ } else if ( calledFatalError && shouldExitProcess ) {
98
101
// we hit an error *after* calling onFatalError - pretty boned at this point, just shut it down
99
102
IS_DEBUG_BUILD &&
100
103
logger . warn ( 'uncaught exception after calling fatal error shutdown callback - this is bad! forcing shutdown' ) ;
101
104
logAndExitProcess ( error ) ;
102
- } else if ( ! caughtSecondError ) {
105
+ } else if ( ! caughtSecondError && shouldExitProcess ) {
103
106
// two cases for how we can hit this branch:
104
107
// - capturing of first error blew up and we just caught the exception from that
105
108
// - quit trying to capture, proceed with shutdown
0 commit comments