@@ -18,7 +18,7 @@ const codes = {};
1818const { kMaxLength } = internalBinding ( 'buffer' ) ;
1919const { defineProperty } = Object ;
2020
21- let useOriginalName = false ;
21+ let excludedStackFn ;
2222
2323// Lazily loaded
2424let util ;
@@ -49,7 +49,15 @@ function lazyBuffer() {
4949// and may have .path and .dest.
5050class SystemError extends Error {
5151 constructor ( key , context ) {
52- super ( ) ;
52+ if ( excludedStackFn === undefined ) {
53+ super ( ) ;
54+ } else {
55+ const limit = Error . stackTraceLimit ;
56+ Error . stackTraceLimit = 0 ;
57+ super ( ) ;
58+ // Reset the limit and setting the name property.
59+ Error . stackTraceLimit = limit ;
60+ }
5361 const prefix = getMessage ( key , [ ] , this ) ;
5462 let message = `${ prefix } : ${ context . syscall } returned ` +
5563 `${ context . code } (${ context . message } )` ;
@@ -148,7 +156,15 @@ function makeSystemErrorWithCode(key) {
148156function makeNodeErrorWithCode ( Base , key ) {
149157 return class NodeError extends Base {
150158 constructor ( ...args ) {
151- super ( ) ;
159+ if ( excludedStackFn === undefined ) {
160+ super ( ) ;
161+ } else {
162+ const limit = Error . stackTraceLimit ;
163+ Error . stackTraceLimit = 0 ;
164+ super ( ) ;
165+ // Reset the limit and setting the name property.
166+ Error . stackTraceLimit = limit ;
167+ }
152168 const message = getMessage ( key , args , this ) ;
153169 Object . defineProperty ( this , 'message' , {
154170 value : message ,
@@ -178,9 +194,30 @@ function makeNodeErrorWithCode(Base, key) {
178194 } ;
179195}
180196
197+ // This function removes unnecessary frames from Node.js core errors.
198+ function hideStackFrames ( fn ) {
199+ return function hidden ( ...args ) {
200+ // Make sure the most outer `hideStackFrames()` function is used.
201+ let setStackFn = false ;
202+ if ( excludedStackFn === undefined ) {
203+ excludedStackFn = hidden ;
204+ setStackFn = true ;
205+ }
206+ try {
207+ return fn ( ...args ) ;
208+ } finally {
209+ if ( setStackFn === true ) {
210+ excludedStackFn = undefined ;
211+ }
212+ }
213+ } ;
214+ }
215+
181216function addCodeToName ( err , name , code ) {
182- if ( useOriginalName ) {
183- return ;
217+ // Set the stack
218+ if ( excludedStackFn !== undefined ) {
219+ // eslint-disable-next-line no-restricted-syntax
220+ Error . captureStackTrace ( err , excludedStackFn ) ;
184221 }
185222 // Add the error code to the name to include it in the stack trace.
186223 err . name = `${ name } [${ code } ]` ;
@@ -308,6 +345,7 @@ function uvException(ctx) {
308345 err [ prop ] = ctx [ prop ] ;
309346 }
310347
348+ // TODO(BridgeAR): Show the `code` property as part of the stack.
311349 err . code = code ;
312350 if ( path ) {
313351 err . path = path ;
@@ -316,7 +354,7 @@ function uvException(ctx) {
316354 err . dest = dest ;
317355 }
318356
319- Error . captureStackTrace ( err , uvException ) ;
357+ Error . captureStackTrace ( err , excludedStackFn || uvException ) ;
320358 return err ;
321359}
322360
@@ -358,7 +396,7 @@ function uvExceptionWithHostPort(err, syscall, address, port) {
358396 ex . port = port ;
359397 }
360398
361- Error . captureStackTrace ( ex , uvExceptionWithHostPort ) ;
399+ Error . captureStackTrace ( ex , excludedStackFn || uvExceptionWithHostPort ) ;
362400 return ex ;
363401}
364402
@@ -386,7 +424,7 @@ function errnoException(err, syscall, original) {
386424 ex . code = ex . errno = code ;
387425 ex . syscall = syscall ;
388426
389- Error . captureStackTrace ( ex , errnoException ) ;
427+ Error . captureStackTrace ( ex , excludedStackFn || errnoException ) ;
390428 return ex ;
391429}
392430
@@ -434,7 +472,7 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
434472 ex . port = port ;
435473 }
436474
437- Error . captureStackTrace ( ex , exceptionWithHostPort ) ;
475+ Error . captureStackTrace ( ex , excludedStackFn || exceptionWithHostPort ) ;
438476 return ex ;
439477}
440478
@@ -473,7 +511,8 @@ function dnsException(code, syscall, hostname) {
473511 if ( hostname ) {
474512 ex . hostname = hostname ;
475513 }
476- Error . captureStackTrace ( ex , dnsException ) ;
514+
515+ Error . captureStackTrace ( ex , excludedStackFn || dnsException ) ;
477516 return ex ;
478517}
479518
@@ -523,21 +562,19 @@ function oneOf(expected, thing) {
523562}
524563
525564module . exports = {
565+ addCodeToName, // Exported for NghttpError
566+ codes,
526567 dnsException,
527568 errnoException,
528569 exceptionWithHostPort,
570+ getMessage,
571+ hideStackFrames,
572+ isStackOverflowError,
529573 uvException,
530574 uvExceptionWithHostPort,
531- isStackOverflowError,
532- getMessage,
533575 SystemError,
534- codes,
535576 // This is exported only to facilitate testing.
536- E,
537- // This allows us to tell the type of the errors without using
538- // instanceof, which is necessary in WPT harness.
539- get useOriginalName ( ) { return useOriginalName ; } ,
540- set useOriginalName ( value ) { useOriginalName = value ; }
577+ E
541578} ;
542579
543580// To declare an error message, use the E(sym, val, def) function above. The sym
@@ -556,7 +593,6 @@ module.exports = {
556593// Note: Please try to keep these in alphabetical order
557594//
558595// Note: Node.js specific errors must begin with the prefix ERR_
559-
560596E ( 'ERR_AMBIGUOUS_ARGUMENT' , 'The "%s" argument is ambiguous. %s' , TypeError ) ;
561597E ( 'ERR_ARG_NOT_ITERABLE' , '%s must be iterable' , TypeError ) ;
562598E ( 'ERR_ASSERTION' , '%s' , Error ) ;
@@ -630,7 +666,10 @@ E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) {
630666} , TypeError ) ;
631667E ( 'ERR_ENCODING_NOT_SUPPORTED' , 'The "%s" encoding is not supported' ,
632668 RangeError ) ;
633- E ( 'ERR_FALSY_VALUE_REJECTION' , 'Promise was rejected with falsy value' , Error ) ;
669+ E ( 'ERR_FALSY_VALUE_REJECTION' , function ( reason ) {
670+ this . reason = reason ;
671+ return 'Promise was rejected with falsy value' ;
672+ } , Error ) ;
634673E ( 'ERR_FS_FILE_TOO_LARGE' , 'File size (%s) is greater than possible Buffer: ' +
635674 `${ kMaxLength } bytes` ,
636675 RangeError ) ;
0 commit comments