@@ -56,6 +56,7 @@ const MainContextError = Error;
56
56
const overrideStackTrace = new WeakMap ( ) ;
57
57
const kNoOverride = Symbol ( 'kNoOverride' ) ;
58
58
let userStackTraceLimit = Error . stackTraceLimit ;
59
+ const nodeInternalPrefix = '__node_internal_' ;
59
60
const prepareStackTrace = ( globalThis , error , trace ) => {
60
61
// API for node internals to override error stack formatting
61
62
// without interfering with userland code.
@@ -65,15 +66,18 @@ const prepareStackTrace = (globalThis, error, trace) => {
65
66
return f ( error , trace ) ;
66
67
}
67
68
68
- for ( let l = trace . length - 1 ; l >= 0 ; l -- ) {
69
- const fn = trace [ l ] . getFunctionName ( ) ;
70
- if ( fn != null && fn . startsWith ( '__node_internal_hidden_' ) ) {
71
- trace . splice ( 0 , l + 1 ) ;
72
- break ;
69
+ const firstFrame = trace [ 0 ] ?. getFunctionName ( ) ;
70
+ if ( firstFrame && firstFrame . startsWith ( nodeInternalPrefix ) ) {
71
+ for ( let l = trace . length - 1 ; l >= 0 ; l -- ) {
72
+ const fn = trace [ l ] . getFunctionName ( ) ;
73
+ if ( fn != null && fn . startsWith ( nodeInternalPrefix ) ) {
74
+ trace . splice ( 0 , l + 1 ) ;
75
+ break ;
76
+ }
73
77
}
78
+ if ( trace . length > userStackTraceLimit )
79
+ trace . splice ( userStackTraceLimit ) ;
74
80
}
75
- if ( trace . length > userStackTraceLimit )
76
- trace . splice ( userStackTraceLimit ) ;
77
81
78
82
const globalOverride =
79
83
maybeOverridePrepareStackTrace ( globalThis , error , trace ) ;
@@ -136,7 +140,7 @@ function lazyBuffer() {
136
140
return buffer ;
137
141
}
138
142
139
- const addCodeToName = hideStackFrames ( function ( err , name , code ) {
143
+ const addCodeToName = hideStackFrames ( function addCodeToName ( err , name , code ) {
140
144
// Set the stack
141
145
err = captureLargerStackTrace ( err ) ;
142
146
// Add the error code to the name to include it in the stack trace.
@@ -309,7 +313,7 @@ function makeNodeErrorWithCode(Base, key) {
309
313
function hideStackFrames ( fn ) {
310
314
// We rename the functions that will be hidden to cut off the stacktrace
311
315
// at the outermost one
312
- const hidden = '__node_internal_hidden_' + fn . name ;
316
+ const hidden = nodeInternalPrefix + fn . name ;
313
317
ObjectDefineProperty ( fn , 'name' , { value : hidden } ) ;
314
318
return fn ;
315
319
}
@@ -380,16 +384,17 @@ function uvErrmapGet(name) {
380
384
return uvBinding . errmap . get ( name ) ;
381
385
}
382
386
383
- function captureLargerStackTrace ( err ) {
384
- userStackTraceLimit = Error . stackTraceLimit ;
385
- Error . stackTraceLimit = Infinity ;
386
- // eslint-disable-next-line no-restricted-syntax
387
- Error . captureStackTrace ( err ) ;
388
- // Reset the limit and setting the name property.
389
- Error . stackTraceLimit = userStackTraceLimit ;
387
+ const captureLargerStackTrace = hideStackFrames (
388
+ function captureLargerStackTrace ( err ) {
389
+ userStackTraceLimit = Error . stackTraceLimit ;
390
+ Error . stackTraceLimit = Infinity ;
391
+ // eslint-disable-next-line no-restricted-syntax
392
+ Error . captureStackTrace ( err ) ;
393
+ // Reset the limit
394
+ Error . stackTraceLimit = userStackTraceLimit ;
390
395
391
- return err ;
392
- }
396
+ return err ;
397
+ } ) ;
393
398
394
399
/**
395
400
* This creates an error compatible with errors produced in the C++
@@ -400,7 +405,7 @@ function captureLargerStackTrace(err) {
400
405
* @param {Object } ctx
401
406
* @returns {Error }
402
407
*/
403
- const uvException = hideStackFrames ( function ( ctx ) {
408
+ const uvException = hideStackFrames ( function uvException ( ctx ) {
404
409
const [ code , uvmsg ] = uvErrmapGet ( ctx . errno ) || uvUnmappedError ;
405
410
let message = `${ code } : ${ ctx . message || uvmsg } , ${ ctx . syscall } ` ;
406
411
@@ -455,8 +460,8 @@ const uvException = hideStackFrames(function(ctx) {
455
460
* @param {number } [port]
456
461
* @returns {Error }
457
462
*/
458
- const uvExceptionWithHostPort =
459
- hideStackFrames ( function ( err , syscall , address , port ) {
463
+ const uvExceptionWithHostPort = hideStackFrames (
464
+ function uvExceptionWithHostPort ( err , syscall , address , port ) {
460
465
const [ code , uvmsg ] = uvErrmapGet ( err ) || uvUnmappedError ;
461
466
const message = `${ syscall } ${ code } : ${ uvmsg } ` ;
462
467
let details = '' ;
@@ -495,28 +500,29 @@ const uvExceptionWithHostPort =
495
500
* @param {string } [original]
496
501
* @returns {Error }
497
502
*/
498
- const errnoException = hideStackFrames ( function ( err , syscall , original ) {
499
- // TODO(joyeecheung): We have to use the type-checked
500
- // getSystemErrorName(err) to guard against invalid arguments from users.
501
- // This can be replaced with [ code ] = errmap.get(err) when this method
502
- // is no longer exposed to user land.
503
- if ( util === undefined ) util = require ( 'util' ) ;
504
- const code = util . getSystemErrorName ( err ) ;
505
- const message = original ?
506
- `${ syscall } ${ code } ${ original } ` : `${ syscall } ${ code } ` ;
503
+ const errnoException = hideStackFrames (
504
+ function errnoException ( err , syscall , original ) {
505
+ // TODO(joyeecheung): We have to use the type-checked
506
+ // getSystemErrorName(err) to guard against invalid arguments from users.
507
+ // This can be replaced with [ code ] = errmap.get(err) when this method
508
+ // is no longer exposed to user land.
509
+ if ( util === undefined ) util = require ( 'util' ) ;
510
+ const code = util . getSystemErrorName ( err ) ;
511
+ const message = original ?
512
+ `${ syscall } ${ code } ${ original } ` : `${ syscall } ${ code } ` ;
507
513
508
- const tmpLimit = Error . stackTraceLimit ;
509
- Error . stackTraceLimit = 0 ;
510
- // eslint-disable-next-line no-restricted-syntax
511
- const ex = new Error ( message ) ;
512
- Error . stackTraceLimit = tmpLimit ;
513
- ex . errno = err ;
514
- ex . code = code ;
515
- ex . syscall = syscall ;
514
+ const tmpLimit = Error . stackTraceLimit ;
515
+ Error . stackTraceLimit = 0 ;
516
+ // eslint-disable-next-line no-restricted-syntax
517
+ const ex = new Error ( message ) ;
518
+ Error . stackTraceLimit = tmpLimit ;
519
+ ex . errno = err ;
520
+ ex . code = code ;
521
+ ex . syscall = syscall ;
516
522
517
- captureLargerStackTrace ( ex ) ;
518
- return ex ;
519
- } ) ;
523
+ captureLargerStackTrace ( ex ) ;
524
+ return ex ;
525
+ } ) ;
520
526
521
527
/**
522
528
* Deprecated, new function is `uvExceptionWithHostPort()`
@@ -529,8 +535,8 @@ const errnoException = hideStackFrames(function(err, syscall, original) {
529
535
* @param {string } [additional]
530
536
* @returns {Error }
531
537
*/
532
- const exceptionWithHostPort =
533
- hideStackFrames ( function ( err , syscall , address , port , additional ) {
538
+ const exceptionWithHostPort = hideStackFrames (
539
+ function exceptionWithHostPort ( err , syscall , address , port , additional ) {
534
540
// TODO(joyeecheung): We have to use the type-checked
535
541
// getSystemErrorName(err) to guard against invalid arguments from users.
536
542
// This can be replaced with [ code ] = errmap.get(err) when this method
0 commit comments