@@ -23,9 +23,16 @@ const { kMaxLength } = process.binding('buffer');
23
23
const { defineProperty } = Object ;
24
24
25
25
// Lazily loaded
26
- var util = null ;
26
+ var util_ = null ;
27
27
var buffer ;
28
28
29
+ function lazyUtil ( ) {
30
+ if ( ! util_ ) {
31
+ util_ = require ( 'util' ) ;
32
+ }
33
+ return util_ ;
34
+ }
35
+
29
36
function makeNodeError ( Base ) {
30
37
return class NodeError extends Base {
31
38
constructor ( key , ...args ) {
@@ -142,6 +149,7 @@ function createErrDiff(actual, expected, operator) {
142
149
var lastPos = 0 ;
143
150
var end = '' ;
144
151
var skipped = false ;
152
+ const util = lazyUtil ( ) ;
145
153
const actualLines = util
146
154
. inspect ( actual , { compact : false } ) . split ( '\n' ) ;
147
155
const expectedLines = util
@@ -262,13 +270,11 @@ class AssertionError extends Error {
262
270
if ( message != null ) {
263
271
super ( message ) ;
264
272
} else {
265
- if ( util === null ) {
266
- util = require ( 'util' ) ;
267
- if ( process . stdout . isTTY && process . stdout . getColorDepth ( ) !== 1 ) {
268
- green = '\u001b[32m' ;
269
- white = '\u001b[39m' ;
270
- red = '\u001b[31m' ;
271
- }
273
+ const util = lazyUtil ( ) ;
274
+ if ( process . stdout . isTTY && process . stdout . getColorDepth ( ) !== 1 ) {
275
+ green = '\u001b[32m' ;
276
+ white = '\u001b[39m' ;
277
+ red = '\u001b[31m' ;
272
278
}
273
279
274
280
if ( actual && actual . stack && actual instanceof Error )
@@ -333,7 +339,7 @@ function message(key, args) {
333
339
if ( typeof msg === 'function' ) {
334
340
fmt = msg ;
335
341
} else {
336
- if ( util === null ) util = require ( 'util' ) ;
342
+ const util = lazyUtil ( ) ;
337
343
fmt = util . format ;
338
344
if ( args === undefined || args . length === 0 )
339
345
return msg ;
@@ -537,8 +543,14 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed');
537
543
E ( 'ERR_INSPECTOR_NOT_AVAILABLE' , 'Inspector is not available' ) ;
538
544
E ( 'ERR_INSPECTOR_NOT_CONNECTED' , 'Session is not connected' ) ;
539
545
E ( 'ERR_INVALID_ARG_TYPE' , invalidArgType ) ;
540
- E ( 'ERR_INVALID_ARG_VALUE' , ( name , value ) =>
541
- `The value "${ String ( value ) } " is invalid for argument "${ name } "` ) ;
546
+ E ( 'ERR_INVALID_ARG_VALUE' , ( name , value , reason = 'is invalid' ) => {
547
+ const util = lazyUtil ( ) ;
548
+ let inspected = util . inspect ( value ) ;
549
+ if ( inspected . length > 128 ) {
550
+ inspected = inspected . slice ( 0 , 128 ) + '...' ;
551
+ }
552
+ return `The argument '${ name } ' ${ reason } . Received ${ inspected } ` ;
553
+ } ) ,
542
554
E ( 'ERR_INVALID_ARRAY_LENGTH' ,
543
555
( name , len , actual ) => {
544
556
internalAssert ( typeof actual === 'number' , 'actual must be a number' ) ;
0 commit comments