diff --git a/doc/api/errors.md b/doc/api/errors.md index 8099bed92cd10e..0f8372409d77ad 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -565,7 +565,7 @@ found [here][online]. ### ERR_INDEX_OUT_OF_RANGE -The `'ERR_INDEX_OUT_OF_RANGE'` error code is used to alert user that index parsed is out of range +The `'ERR_INDEX_OUT_OF_RANGE'` error code is used when a given index is out of the accepted range. diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index ca68a27a90fe7a..a2095c31c00e58 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -963,7 +963,8 @@ assert.throws(() => { const a = Buffer.alloc(1); const b = Buffer.alloc(1); a.copy(b, 0, 0x100000000, 0x100000001); -}, /out of range index/); +}, common.expectsError( + {code: undefined, type: RangeError, message: 'Index out of range'})); // Unpooled buffer (replaces SlowBuffer) { diff --git a/test/parallel/test-buffer-compare-offset.js b/test/parallel/test-buffer-compare-offset.js index 037e82e055cb91..e5c7a691102cbc 100644 --- a/test/parallel/test-buffer-compare-offset.js +++ b/test/parallel/test-buffer-compare-offset.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]); @@ -57,7 +57,7 @@ assert.strictEqual(1, a.compare(b, Infinity, -Infinity)); // zero length target because default for targetEnd <= targetSource assert.strictEqual(1, a.compare(b, '0xff')); -const oor = /out of range index/; +const oor = common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}); assert.throws(() => a.compare(b, 0, 100, 0), oor); assert.throws(() => a.compare(b, 0, 1, 0, 100), oor); diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index 46ebc79b620727..d73aa006a93628 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -1,7 +1,8 @@ +// Flags: --expose-internals 'use strict'; - -require('../common'); +const common = require('../common'); const assert = require('assert'); +const errors = require('internal/errors'); const SIZE = 28; const buf1 = Buffer.allocUnsafe(SIZE); @@ -191,25 +192,30 @@ deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]); // Check exceptions -assert.throws(() => buf1.fill(0, -1), /^RangeError: Out of range index$/); -assert.throws(() => - buf1.fill(0, 0, buf1.length + 1), - /^RangeError: Out of range index$/); -assert.throws(() => buf1.fill('', -1), /^RangeError: Out of range index$/); -assert.throws(() => - buf1.fill('', 0, buf1.length + 1), - /^RangeError: Out of range index$/); -assert.throws(() => - buf1.fill('a', 0, buf1.length, 'node rocks!'), - /^TypeError: Unknown encoding: node rocks!$/); -assert.throws(() => - buf1.fill('a', 0, 0, NaN), - /^TypeError: encoding must be a string$/); -assert.throws(() => - buf1.fill('a', 0, 0, null), - /^TypeError: encoding must be a string$/); -assert.throws(() => - buf1.fill('a', 0, 0, 'foo'), /^TypeError: Unknown encoding: foo$/); +assert.throws( + () => buf1.fill(0, -1), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); +assert.throws( + () => buf1.fill(0, 0, buf1.length + 1), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); +assert.throws( + () => buf1.fill('', -1), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); +assert.throws( + () => buf1.fill('', 0, buf1.length + 1), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); +assert.throws( + () => buf1.fill('a', 0, buf1.length, 'node rocks!'), + /^TypeError: Unknown encoding: node rocks!$/); +assert.throws( + () => buf1.fill('a', 0, 0, NaN), + /^TypeError: encoding must be a string$/); +assert.throws( + () => buf1.fill('a', 0, 0, null), + /^TypeError: encoding must be a string$/); +assert.throws( + () => buf1.fill('a', 0, 0, 'foo'), + /^TypeError: Unknown encoding: foo$/); function genBuffer(size, args) { @@ -241,7 +247,7 @@ function writeToFill(string, offset, end, encoding) { } if (offset < 0 || end > buf2.length) - throw new RangeError('Out of range index'); + throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE'); if (end <= offset) return buf2; @@ -279,12 +285,12 @@ function testBufs(string, offset, length, encoding) { } // Make sure these throw. -assert.throws(() => - Buffer.allocUnsafe(8).fill('a', -1), - /^RangeError: Out of range index$/); -assert.throws(() => - Buffer.allocUnsafe(8).fill('a', 0, 9), - /^RangeError: Out of range index$/); +assert.throws( + () => Buffer.allocUnsafe(8).fill('a', -1), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); +assert.throws( + () => Buffer.allocUnsafe(8).fill('a', 0, 9), + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})); // Make sure this doesn't hang indefinitely. Buffer.allocUnsafe(8).fill(''); @@ -350,7 +356,8 @@ Buffer.alloc(8, ''); } }; Buffer.alloc(1).fill(Buffer.alloc(1), start, 1); - }, /out of range index/); + }, common.expectsError( + {code: undefined, type: RangeError, message: 'Index out of range'})); // Make sure -1 is making it to Buffer::Fill(). assert.ok(elseWasLast, 'internal API changed, -1 no longer in correct location'); @@ -360,7 +367,8 @@ Buffer.alloc(8, ''); // around. assert.throws(() => { process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1); -}, /out of range index/); +}, common.expectsError( + {code: undefined, type: RangeError, message: 'Index out of range'})); // Make sure "end" is properly checked, even if it's magically mangled using // Symbol.toPrimitive. @@ -383,7 +391,8 @@ assert.throws(() => { } }; Buffer.alloc(1).fill(Buffer.alloc(1), 0, end); - }, /^RangeError: out of range index$/); + }, common.expectsError( + {code: undefined, type: RangeError, message: 'Index out of range'})); // Make sure -1 is making it to Buffer::Fill(). assert.ok(elseWasLast, 'internal API changed, -1 no longer in correct location'); @@ -393,7 +402,8 @@ assert.throws(() => { // around. assert.throws(() => { process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1); -}, /out of range index/); +}, common.expectsError( + { code: undefined, type: RangeError, message: 'Index out of range'})); // Test that bypassing 'length' won't cause an abort. assert.throws(() => { @@ -403,7 +413,8 @@ assert.throws(() => { enumerable: true }); buf.fill(''); -}, /^RangeError: out of range index$/); +}, common.expectsError( + { code: undefined, type: RangeError, message: 'Index out of range'})); assert.deepStrictEqual( Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'), diff --git a/test/parallel/test-buffer-read.js b/test/parallel/test-buffer-read.js index ad80ee4d799818..e55bc3435a6dcc 100644 --- a/test/parallel/test-buffer-read.js +++ b/test/parallel/test-buffer-read.js @@ -10,7 +10,7 @@ function read(buff, funx, args, expected) { assert.strictEqual(buff[funx](...args), expected); assert.throws( () => buff[funx](-1), - common.expectsError('ERR_INDEX_OUT_OF_RANGE') + common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}) ); assert.doesNotThrow( diff --git a/test/parallel/test-buffer-write-noassert.js b/test/parallel/test-buffer-write-noassert.js index 9a5e4e6671fad9..0730aaa3544983 100644 --- a/test/parallel/test-buffer-write-noassert.js +++ b/test/parallel/test-buffer-write-noassert.js @@ -4,7 +4,7 @@ const assert = require('assert'); // testing buffer write functions -const outOfRange = /^RangeError: (?:Index )?out of range(?: index)?$/; +const outOfRange = /^RangeError\b.*\bIndex out of range$/; function write(funx, args, result, res) { {