1+ // Flags: --expose-internals
12'use strict' ;
2-
3- require ( '../common' ) ;
3+ const common = require ( '../common' ) ;
44const assert = require ( 'assert' ) ;
5+ const errors = require ( 'internal/errors' ) ;
56const SIZE = 28 ;
67
78const buf1 = Buffer . allocUnsafe ( SIZE ) ;
@@ -191,25 +192,30 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
191192
192193
193194// Check exceptions
194- assert . throws ( ( ) => buf1 . fill ( 0 , - 1 ) , / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
195- assert . throws ( ( ) =>
196- buf1 . fill ( 0 , 0 , buf1 . length + 1 ) ,
197- / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
198- assert . throws ( ( ) => buf1 . fill ( '' , - 1 ) , / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
199- assert . throws ( ( ) =>
200- buf1 . fill ( '' , 0 , buf1 . length + 1 ) ,
201- / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
202- assert . throws ( ( ) =>
203- buf1 . fill ( 'a' , 0 , buf1 . length , 'node rocks!' ) ,
204- / ^ T y p e E r r o r : U n k n o w n e n c o d i n g : n o d e r o c k s ! $ / ) ;
205- assert . throws ( ( ) =>
206- buf1 . fill ( 'a' , 0 , 0 , NaN ) ,
207- / ^ T y p e E r r o r : e n c o d i n g m u s t b e a s t r i n g $ / ) ;
208- assert . throws ( ( ) =>
209- buf1 . fill ( 'a' , 0 , 0 , null ) ,
210- / ^ T y p e E r r o r : e n c o d i n g m u s t b e a s t r i n g $ / ) ;
211- assert . throws ( ( ) =>
212- buf1 . fill ( 'a' , 0 , 0 , 'foo' ) , / ^ T y p e E r r o r : U n k n o w n e n c o d i n g : f o o $ / ) ;
195+ assert . throws (
196+ ( ) => buf1 . fill ( 0 , - 1 ) ,
197+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
198+ assert . throws (
199+ ( ) => buf1 . fill ( 0 , 0 , buf1 . length + 1 ) ,
200+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
201+ assert . throws (
202+ ( ) => buf1 . fill ( '' , - 1 ) ,
203+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
204+ assert . throws (
205+ ( ) => buf1 . fill ( '' , 0 , buf1 . length + 1 ) ,
206+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
207+ assert . throws (
208+ ( ) => buf1 . fill ( 'a' , 0 , buf1 . length , 'node rocks!' ) ,
209+ / ^ T y p e E r r o r : U n k n o w n e n c o d i n g : n o d e r o c k s ! $ / ) ;
210+ assert . throws (
211+ ( ) => buf1 . fill ( 'a' , 0 , 0 , NaN ) ,
212+ / ^ T y p e E r r o r : e n c o d i n g m u s t b e a s t r i n g $ / ) ;
213+ assert . throws (
214+ ( ) => buf1 . fill ( 'a' , 0 , 0 , null ) ,
215+ / ^ T y p e E r r o r : e n c o d i n g m u s t b e a s t r i n g $ / ) ;
216+ assert . throws (
217+ ( ) => buf1 . fill ( 'a' , 0 , 0 , 'foo' ) ,
218+ / ^ T y p e E r r o r : U n k n o w n e n c o d i n g : f o o $ / ) ;
213219
214220
215221function genBuffer ( size , args ) {
@@ -241,7 +247,7 @@ function writeToFill(string, offset, end, encoding) {
241247 }
242248
243249 if ( offset < 0 || end > buf2 . length )
244- throw new RangeError ( 'Out of range index ' ) ;
250+ throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE ' ) ;
245251
246252 if ( end <= offset )
247253 return buf2 ;
@@ -279,12 +285,12 @@ function testBufs(string, offset, length, encoding) {
279285}
280286
281287// Make sure these throw.
282- assert . throws ( ( ) =>
283- Buffer . allocUnsafe ( 8 ) . fill ( 'a' , - 1 ) ,
284- / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
285- assert . throws ( ( ) =>
286- Buffer . allocUnsafe ( 8 ) . fill ( 'a' , 0 , 9 ) ,
287- / ^ R a n g e E r r o r : O u t o f r a n g e i n d e x $ / ) ;
288+ assert . throws (
289+ ( ) => Buffer . allocUnsafe ( 8 ) . fill ( 'a' , - 1 ) ,
290+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
291+ assert . throws (
292+ ( ) => Buffer . allocUnsafe ( 8 ) . fill ( 'a' , 0 , 9 ) ,
293+ common . expectsError ( { code : 'ERR_INDEX_OUT_OF_RANGE' } ) ) ;
288294
289295// Make sure this doesn't hang indefinitely.
290296Buffer . allocUnsafe ( 8 ) . fill ( '' ) ;
@@ -350,7 +356,8 @@ Buffer.alloc(8, '');
350356 }
351357 } ;
352358 Buffer . alloc ( 1 ) . fill ( Buffer . alloc ( 1 ) , start , 1 ) ;
353- } , / o u t o f r a n g e i n d e x / ) ;
359+ } , common . expectsError (
360+ { code : undefined , type : RangeError , message : 'Index out of range' } ) ) ;
354361 // Make sure -1 is making it to Buffer::Fill().
355362 assert . ok ( elseWasLast ,
356363 'internal API changed, -1 no longer in correct location' ) ;
@@ -360,7 +367,8 @@ Buffer.alloc(8, '');
360367// around.
361368assert . throws ( ( ) => {
362369 process . binding ( 'buffer' ) . fill ( Buffer . alloc ( 1 ) , 1 , - 1 , 0 , 1 ) ;
363- } , / o u t o f r a n g e i n d e x / ) ;
370+ } , common . expectsError (
371+ { code : undefined , type : RangeError , message : 'Index out of range' } ) ) ;
364372
365373// Make sure "end" is properly checked, even if it's magically mangled using
366374// Symbol.toPrimitive.
@@ -383,7 +391,8 @@ assert.throws(() => {
383391 }
384392 } ;
385393 Buffer . alloc ( 1 ) . fill ( Buffer . alloc ( 1 ) , 0 , end ) ;
386- } , / ^ R a n g e E r r o r : o u t o f r a n g e i n d e x $ / ) ;
394+ } , common . expectsError (
395+ { code : undefined , type : RangeError , message : 'Index out of range' } ) ) ;
387396 // Make sure -1 is making it to Buffer::Fill().
388397 assert . ok ( elseWasLast ,
389398 'internal API changed, -1 no longer in correct location' ) ;
@@ -393,7 +402,8 @@ assert.throws(() => {
393402// around.
394403assert . throws ( ( ) => {
395404 process . binding ( 'buffer' ) . fill ( Buffer . alloc ( 1 ) , 1 , 1 , - 2 , 1 ) ;
396- } , / o u t o f r a n g e i n d e x / ) ;
405+ } , common . expectsError (
406+ { code : undefined , type : RangeError , message : 'Index out of range' } ) ) ;
397407
398408// Test that bypassing 'length' won't cause an abort.
399409assert . throws ( ( ) => {
@@ -403,7 +413,8 @@ assert.throws(() => {
403413 enumerable : true
404414 } ) ;
405415 buf . fill ( '' ) ;
406- } , / ^ R a n g e E r r o r : o u t o f r a n g e i n d e x $ / ) ;
416+ } , common . expectsError (
417+ { code : undefined , type : RangeError , message : 'Index out of range' } ) ) ;
407418
408419assert . deepStrictEqual (
409420 Buffer . allocUnsafeSlow ( 16 ) . fill ( 'ab' , 'utf16le' ) ,
0 commit comments