@@ -355,21 +355,35 @@ function getErrorSource(error) {
355
355
return null ;
356
356
}
357
357
358
- // Mimic node's stack trace printing when an exception escapes the process
359
- function handleUncaughtExceptions ( error ) {
360
- if ( ! error || ! error . stack ) {
361
- console . error ( 'Uncaught exception:' , error ) ;
362
- } else {
363
- var source = getErrorSource ( error ) ;
364
- if ( source !== null ) {
365
- console . error ( ) ;
366
- console . error ( source ) ;
367
- }
368
- console . error ( error . stack ) ;
358
+ function printErrorAndExit ( error ) {
359
+ var source = getErrorSource ( error ) ;
360
+
361
+ if ( source ) {
362
+ console . error ( ) ;
363
+ console . error ( source ) ;
369
364
}
365
+
366
+ console . error ( error . stack ) ;
370
367
process . exit ( 1 ) ;
371
368
}
372
369
370
+ function shimEmitUncaughtException ( ) {
371
+ var origEmit = process . emit ;
372
+
373
+ process . emit = function ( type ) {
374
+ if ( type === 'uncaughtException' ) {
375
+ var hasStack = ( arguments [ 1 ] && arguments [ 1 ] . stack ) ;
376
+ var hasListeners = ( this . listeners ( type ) . length > 0 ) ;
377
+
378
+ if ( hasStack && ! hasListeners ) {
379
+ return printErrorAndExit ( arguments [ 1 ] ) ;
380
+ }
381
+ }
382
+
383
+ return origEmit . apply ( this , arguments ) ;
384
+ }
385
+ }
386
+
373
387
exports . wrapCallSite = wrapCallSite ;
374
388
exports . getErrorSource = getErrorSource ;
375
389
exports . mapSourcePosition = mapSourcePosition ;
@@ -405,7 +419,7 @@ exports.install = function(options) {
405
419
// generated JavaScript code will be shown above the stack trace instead of
406
420
// the original source code.
407
421
if ( installHandler && ! isInBrowser ( ) ) {
408
- process . on ( 'uncaughtException' , handleUncaughtExceptions ) ;
422
+ shimEmitUncaughtException ( ) ;
409
423
}
410
424
}
411
425
} ;
0 commit comments