errors,buffer: port errors to internal/errors

PR-URL: https://github.com/nodejs/node/pull/13976
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
starkwang 2017-06-28 23:58:39 -04:00 committed by Refael Ackermann
parent 1562fb9ea7
commit dbfe8c4ea2
No known key found for this signature in database
GPG key ID: CD704BD80FDDDB64
28 changed files with 342 additions and 143 deletions

View file

@ -1,6 +1,6 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const LENGTH = 16;
@ -65,16 +65,16 @@ b.writeDoubleBE(11.11, 0, true);
buf[0] = 9;
assert.strictEqual(ab[1], 9);
assert.throws(() => Buffer.from(ab.buffer, 6), (err) => {
assert(err instanceof RangeError);
assert(/'offset' is out of bounds/.test(err.message));
return true;
});
assert.throws(() => Buffer.from(ab.buffer, 3, 6), (err) => {
assert(err instanceof RangeError);
assert(/'length' is out of bounds/.test(err.message));
return true;
});
assert.throws(() => Buffer.from(ab.buffer, 6), common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"offset" is outside of buffer bounds'
}));
assert.throws(() => Buffer.from(ab.buffer, 3, 6), common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"length" is outside of buffer bounds'
}));
}
// Test the deprecated Buffer() version also
@ -93,16 +93,16 @@ b.writeDoubleBE(11.11, 0, true);
buf[0] = 9;
assert.strictEqual(ab[1], 9);
assert.throws(() => Buffer(ab.buffer, 6), (err) => {
assert(err instanceof RangeError);
assert(/'offset' is out of bounds/.test(err.message));
return true;
});
assert.throws(() => Buffer(ab.buffer, 3, 6), (err) => {
assert(err instanceof RangeError);
assert(/'length' is out of bounds/.test(err.message));
return true;
});
assert.throws(() => Buffer(ab.buffer, 6), common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"offset" is outside of buffer bounds'
}));
assert.throws(() => Buffer(ab.buffer, 3, 6), common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"length" is outside of buffer bounds'
}));
}
{
@ -118,10 +118,13 @@ b.writeDoubleBE(11.11, 0, true);
assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));
// If byteOffset is Infinity, throw.
assert.throws(
() => { Buffer.from(ab, Infinity); },
/^RangeError: 'offset' is out of bounds$/
);
assert.throws(() => {
Buffer.from(ab, Infinity);
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"offset" is outside of buffer bounds'
}));
}
{
@ -137,8 +140,11 @@ b.writeDoubleBE(11.11, 0, true);
assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));
//If length is Infinity, throw.
assert.throws(
() => { Buffer.from(ab, 0, Infinity); },
/^RangeError: 'length' is out of bounds$/
);
assert.throws(() => {
Buffer.from(ab, 0, Infinity);
}, common.expectsError({
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
type: RangeError,
message: '"length" is outside of buffer bounds'
}));
}