lib: port errors to new system

This is a first batch of updates that touches non-underscored modules in
lib.

PR-URL: https://github.com/nodejs/node/pull/19034
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Michaël Zasso 2018-02-27 14:55:32 +01:00
parent 023f49c5a9
commit 1e8d110e64
31 changed files with 377 additions and 418 deletions

View file

@ -57,7 +57,16 @@ const {
const {
pendingDeprecation
} = process.binding('config');
const errors = require('internal/errors');
const {
ERR_BUFFER_OUT_OF_BOUNDS,
ERR_INDEX_OUT_OF_RANGE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_BUFFER_SIZE,
ERR_INVALID_OPT_VALUE,
ERR_NO_LONGER_SUPPORTED,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
const internalBuffer = require('internal/buffer');
@ -166,9 +175,7 @@ function Buffer(arg, encodingOrOffset, length) {
// Common case.
if (typeof arg === 'number') {
if (typeof encodingOrOffset === 'string') {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'string', 'string', arg
);
throw new ERR_INVALID_ARG_TYPE('string', 'string', arg);
}
return Buffer.alloc(arg);
}
@ -197,8 +204,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
return fromArrayBuffer(value, encodingOrOffset, length);
if (value === null || value === undefined) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
throw new ERR_INVALID_ARG_TYPE(
'first argument',
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
value
@ -206,9 +212,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
}
if (typeof value === 'number') {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'value', 'not number', value
);
throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
}
const valueOf = value.valueOf && value.valueOf();
@ -225,8 +229,7 @@ Buffer.from = function from(value, encodingOrOffset, length) {
length);
}
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE',
throw new ERR_INVALID_ARG_TYPE(
'first argument',
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
value
@ -242,9 +245,9 @@ function assertSize(size) {
let err = null;
if (typeof size !== 'number') {
err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'size', 'number', size);
err = new ERR_INVALID_ARG_TYPE('size', 'number', size);
} else if (size < 0 || size > kMaxLength) {
err = new errors.RangeError('ERR_INVALID_OPT_VALUE', 'size', size);
err = new ERR_INVALID_OPT_VALUE.RangeError('size', size);
}
if (err !== null) {
@ -323,7 +326,7 @@ function fromString(string, encoding) {
} else {
length = byteLength(string, encoding, true);
if (length === -1)
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
throw new ERR_UNKNOWN_ENCODING(encoding);
if (string.length === 0)
return new FastBuffer();
}
@ -365,7 +368,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
const maxLength = obj.byteLength - byteOffset;
if (maxLength < 0)
throw new errors.RangeError('ERR_BUFFER_OUT_OF_BOUNDS', 'offset');
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
if (length === undefined) {
length = maxLength;
@ -374,7 +377,7 @@ function fromArrayBuffer(obj, byteOffset, length) {
length = +length;
if (length > 0) {
if (length > maxLength)
throw new errors.RangeError('ERR_BUFFER_OUT_OF_BOUNDS', 'length');
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
} else {
length = 0;
}
@ -414,9 +417,7 @@ Buffer.isBuffer = function isBuffer(b) {
Buffer.compare = function compare(a, b) {
if (!isUint8Array(a) || !isUint8Array(b)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', ['buf1', 'buf2'], ['Buffer', 'Uint8Array']
);
throw new ERR_INVALID_ARG_TYPE(['buf1', 'buf2'], ['Buffer', 'Uint8Array']);
}
if (a === b) {
@ -435,9 +436,7 @@ Buffer[kIsEncodingSymbol] = Buffer.isEncoding;
Buffer.concat = function concat(list, length) {
var i;
if (!Array.isArray(list)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'list', ['Array', 'Buffer', 'Uint8Array']
);
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
}
if (list.length === 0)
@ -456,9 +455,7 @@ Buffer.concat = function concat(list, length) {
for (i = 0; i < list.length; i++) {
var buf = list[i];
if (!isUint8Array(buf)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'list', ['Array', 'Buffer', 'Uint8Array']
);
throw new ERR_INVALID_ARG_TYPE('list', ['Array', 'Buffer', 'Uint8Array']);
}
_copy(buf, buffer, pos);
pos += buf.length;
@ -492,9 +489,8 @@ function byteLength(string, encoding) {
return string.byteLength;
}
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'string',
['string', 'Buffer', 'ArrayBuffer'], string
throw new ERR_INVALID_ARG_TYPE(
'string', ['string', 'Buffer', 'ArrayBuffer'], string
);
}
@ -609,7 +605,7 @@ function stringSlice(buf, encoding, start, end) {
return buf.ucs2Slice(start, end);
break;
}
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
throw new ERR_UNKNOWN_ENCODING(encoding);
}
Buffer.prototype.copy =
@ -650,10 +646,7 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
Buffer.prototype.equals = function equals(b) {
if (!isUint8Array(b)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'otherBuffer',
['Buffer', 'Uint8Array'], b
);
throw new ERR_INVALID_ARG_TYPE('otherBuffer', ['Buffer', 'Uint8Array'], b);
}
if (this === b)
return true;
@ -678,10 +671,7 @@ Buffer.prototype.compare = function compare(target,
thisStart,
thisEnd) {
if (!isUint8Array(target)) {
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'target',
['Buffer', 'Uint8Array'], target
);
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
}
if (arguments.length === 1)
return _compare(this, target);
@ -689,28 +679,28 @@ Buffer.prototype.compare = function compare(target,
if (start === undefined)
start = 0;
else if (start < 0)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
else
start >>>= 0;
if (end === undefined)
end = target.length;
else if (end > target.length)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
else
end >>>= 0;
if (thisStart === undefined)
thisStart = 0;
else if (thisStart < 0)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
else
thisStart >>>= 0;
if (thisEnd === undefined)
thisEnd = this.length;
else if (thisEnd > this.length)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
else
thisEnd >>>= 0;
@ -759,9 +749,8 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
return indexOfNumber(buffer, val, byteOffset, dir);
}
throw new errors.TypeError(
'ERR_INVALID_ARG_TYPE', 'value',
['string', 'Buffer', 'Uint8Array'], val
throw new ERR_INVALID_ARG_TYPE(
'value', ['string', 'Buffer', 'Uint8Array'], val
);
}
@ -787,7 +776,7 @@ function slowIndexOf(buffer, val, byteOffset, encoding, dir) {
default:
if (loweredCase) {
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
throw new ERR_UNKNOWN_ENCODING(encoding);
}
encoding = ('' + encoding).toLowerCase();
@ -830,10 +819,9 @@ function _fill(buf, val, start, end, encoding) {
const normalizedEncoding = normalizeEncoding(encoding);
if (normalizedEncoding === undefined) {
if (typeof encoding !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'encoding', 'string',
encoding);
throw new ERR_INVALID_ARG_TYPE('encoding', 'string', encoding);
}
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
throw new ERR_UNKNOWN_ENCODING(encoding);
}
if (val.length === 0) {
@ -861,11 +849,11 @@ function _fill(buf, val, start, end, encoding) {
// Invalid ranges are not set to a default, so can range check early.
if (end === undefined) {
if (start < 0)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
end = buf.length;
} else {
if (start < 0 || end > buf.length || end < 0)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INDEX_OUT_OF_RANGE();
end = end >>> 0;
}
start = start >>> 0;
@ -876,8 +864,8 @@ function _fill(buf, val, start, end, encoding) {
const res = bindingFill(buf, val, start, end, encoding);
if (res < 0) {
if (res === -1)
throw new errors.TypeError('ERR_INVALID_ARG_VALUE', 'value', val);
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
throw new ERR_INVALID_ARG_VALUE('value', val);
throw new ERR_INDEX_OUT_OF_RANGE();
}
return buf;
@ -909,13 +897,12 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
length = remaining;
if (string.length > 0 && (length < 0 || offset < 0))
throw new errors.RangeError('ERR_BUFFER_OUT_OF_BOUNDS', 'length', true);
throw new ERR_BUFFER_OUT_OF_BOUNDS('length', true);
} else {
// if someone is still calling the obsolete form of write(), tell them.
// we don't want eg buf.write("foo", "utf8", 10) to silently turn into
// buf.write("foo", "utf8"), so we can't ignore extra args
throw new errors.Error(
'ERR_NO_LONGER_SUPPORTED',
throw new ERR_NO_LONGER_SUPPORTED(
'Buffer.write(string, encoding, offset[, length])'
);
}
@ -964,7 +951,7 @@ Buffer.prototype.write = function write(string, offset, length, encoding) {
return this.hexWrite(string, offset, length);
break;
}
throw new errors.TypeError('ERR_UNKNOWN_ENCODING', encoding);
throw new ERR_UNKNOWN_ENCODING(encoding);
};
Buffer.prototype.toJSON = function toJSON() {
@ -1015,7 +1002,7 @@ Buffer.prototype.swap16 = function swap16() {
// dropping down to the native code is faster.
const len = this.length;
if (len % 2 !== 0)
throw new errors.RangeError('ERR_INVALID_BUFFER_SIZE', '16-bits');
throw new ERR_INVALID_BUFFER_SIZE('16-bits');
if (len < 128) {
for (var i = 0; i < len; i += 2)
swap(this, i, i + 1);
@ -1030,7 +1017,7 @@ Buffer.prototype.swap32 = function swap32() {
// dropping down to the native code is faster.
const len = this.length;
if (len % 4 !== 0)
throw new errors.RangeError('ERR_INVALID_BUFFER_SIZE', '32-bits');
throw new ERR_INVALID_BUFFER_SIZE('32-bits');
if (len < 192) {
for (var i = 0; i < len; i += 4) {
swap(this, i, i + 3);
@ -1047,7 +1034,7 @@ Buffer.prototype.swap64 = function swap64() {
// dropping down to the native code is faster.
const len = this.length;
if (len % 8 !== 0)
throw new errors.RangeError('ERR_INVALID_BUFFER_SIZE', '64-bits');
throw new ERR_INVALID_BUFFER_SIZE('64-bits');
if (len < 192) {
for (var i = 0; i < len; i += 8) {
swap(this, i, i + 7);
@ -1073,8 +1060,8 @@ if (process.binding('config').hasIntl) {
// Buffer instance.
transcode = function transcode(source, fromEncoding, toEncoding) {
if (!isUint8Array(source)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'source',
['Buffer', 'Uint8Array'], source);
throw new ERR_INVALID_ARG_TYPE('source',
['Buffer', 'Uint8Array'], source);
}
if (source.length === 0) return Buffer.alloc(0);