lib: remove unused internal error constructors

PR-URL: https://github.com/nodejs/node/pull/19203
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Michaël Zasso 2018-03-06 14:17:37 +01:00
parent a910320ef3
commit 49963f4da9
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
4 changed files with 32 additions and 123 deletions

View File

@ -36,6 +36,8 @@ const fs = exports;
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
const errors = require('internal/errors'); const errors = require('internal/errors');
const { const {
ERR_FS_WATCHER_ALREADY_STARTED,
ERR_FS_WATCHER_NOT_STARTED,
ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK, ERR_INVALID_CALLBACK,
ERR_OUT_OF_RANGE ERR_OUT_OF_RANGE
@ -1349,7 +1351,7 @@ FSWatcher.prototype.start = function(filename,
encoding) { encoding) {
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent'); lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
if (this._handle.initialized) { if (this._handle.initialized) {
throw new errors.Error('ERR_FS_WATCHER_ALREADY_STARTED'); throw new ERR_FS_WATCHER_ALREADY_STARTED();
} }
filename = getPathFromURL(filename); filename = getPathFromURL(filename);
@ -1373,7 +1375,7 @@ FSWatcher.prototype.start = function(filename,
FSWatcher.prototype.close = function() { FSWatcher.prototype.close = function() {
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent'); lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
if (!this._handle.initialized) { if (!this._handle.initialized) {
throw new errors.Error('ERR_FS_WATCHER_NOT_STARTED'); throw new ERR_FS_WATCHER_NOT_STARTED();
} }
this._handle.close(); this._handle.close();
}; };

View File

@ -125,7 +125,6 @@ function makeNodeErrorWithCode(Base, key) {
// *only* to allow for testing. // *only* to allow for testing.
function E(sym, val, def, ...otherClasses) { function E(sym, val, def, ...otherClasses) {
messages.set(sym, val); messages.set(sym, val);
if (def === undefined) return;
def = makeNodeErrorWithCode(def, sym); def = makeNodeErrorWithCode(def, sym);
if (otherClasses.length !== 0) { if (otherClasses.length !== 0) {
otherClasses.forEach((clazz) => { otherClasses.forEach((clazz) => {
@ -572,10 +571,6 @@ module.exports = exports = {
exceptionWithHostPort, exceptionWithHostPort,
uvException, uvException,
message, message,
Error: makeNodeError(Error),
TypeError: makeNodeError(TypeError),
RangeError: makeNodeError(RangeError),
URIError: makeNodeError(URIError),
AssertionError, AssertionError,
SystemError, SystemError,
codes, codes,
@ -583,12 +578,13 @@ module.exports = exports = {
errorCache: new Map() // This is in here only to facilitate testing. errorCache: new Map() // This is in here only to facilitate testing.
}; };
// To declare an error message, use the E(sym, val) function above. The sym // To declare an error message, use the E(sym, val, def) function above. The sym
// must be an upper case string. The val can be either a function or a string. // must be an upper case string. The val can be either a function or a string.
// The def must be an error class.
// The return value of the function must be a string. // The return value of the function must be a string.
// Examples: // Examples:
// E('EXAMPLE_KEY1', 'This is the error value'); // E('EXAMPLE_KEY1', 'This is the error value', Error);
// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`); // E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`, RangeError);
// //
// Once an error code has been assigned, the code itself MUST NOT change and // Once an error code has been assigned, the code itself MUST NOT change and
// any given error code must never be reused to identify a different error. // any given error code must never be reused to identify a different error.
@ -858,7 +854,6 @@ E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
'stream.unshift() after end event', Error); 'stream.unshift() after end event', Error);
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error); E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error); E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error);
E('ERR_SYSTEM_ERROR', sysError);
E('ERR_TLS_CERT_ALTNAME_INVALID', E('ERR_TLS_CERT_ALTNAME_INVALID',
'Hostname/IP does not match certificate\'s altnames: %s', Error); 'Hostname/IP does not match certificate\'s altnames: %s', Error);
E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error); E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error);
@ -934,6 +929,7 @@ function sysError(code, syscall, path, dest,
} }
return message; return message;
} }
messages.set('ERR_SYSTEM_ERROR', sysError);
function invalidArgType(name, expected, actual) { function invalidArgType(name, expected, actual) {
internalAssert(name, 'name is required'); internalAssert(name, 'name is required');

View File

@ -5,15 +5,12 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const errors = require('internal/errors'); const errors = require('internal/errors');
function invalidKey(key) { errors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
return new RegExp(`^An invalid error message key was used: ${key}\\.$`); Error, TypeError, RangeError);
} errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s');
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
{ {
const err = new errors.Error('TEST_ERROR_1', 'test'); const err = new errors.codes.TEST_ERROR_1('test');
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
assert.strictEqual(err.message, 'Error for testing purposes: test'); assert.strictEqual(err.message, 'Error for testing purposes: test');
@ -21,7 +18,7 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
} }
{ {
const err = new errors.TypeError('TEST_ERROR_1', 'test'); const err = new errors.codes.TEST_ERROR_1.TypeError('test');
assert(err instanceof TypeError); assert(err instanceof TypeError);
assert.strictEqual(err.name, 'TypeError [TEST_ERROR_1]'); assert.strictEqual(err.name, 'TypeError [TEST_ERROR_1]');
assert.strictEqual(err.message, 'Error for testing purposes: test'); assert.strictEqual(err.message, 'Error for testing purposes: test');
@ -29,7 +26,7 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
} }
{ {
const err = new errors.RangeError('TEST_ERROR_1', 'test'); const err = new errors.codes.TEST_ERROR_1.RangeError('test');
assert(err instanceof RangeError); assert(err instanceof RangeError);
assert.strictEqual(err.name, 'RangeError [TEST_ERROR_1]'); assert.strictEqual(err.name, 'RangeError [TEST_ERROR_1]');
assert.strictEqual(err.message, 'Error for testing purposes: test'); assert.strictEqual(err.message, 'Error for testing purposes: test');
@ -37,7 +34,7 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
} }
{ {
const err = new errors.Error('TEST_ERROR_2', 'abc', 'xyz'); const err = new errors.codes.TEST_ERROR_2('abc', 'xyz');
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual(err.name, 'Error [TEST_ERROR_2]'); assert.strictEqual(err.name, 'Error [TEST_ERROR_2]');
assert.strictEqual(err.message, 'abc xyz'); assert.strictEqual(err.message, 'abc xyz');
@ -45,113 +42,27 @@ errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
} }
{ {
const err = new errors.Error('TEST_ERROR_1'); const err = new errors.codes.TEST_ERROR_1();
assert(err instanceof Error); assert(err instanceof Error);
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
assert.strictEqual(err.message, 'Error for testing purposes: %s'); assert.strictEqual(err.message, 'Error for testing purposes: %s');
assert.strictEqual(err.code, 'TEST_ERROR_1'); assert.strictEqual(err.code, 'TEST_ERROR_1');
} }
common.expectsError(
() => new errors.Error('TEST_FOO_KEY'),
{
code: 'ERR_ASSERTION',
message: invalidKey('TEST_FOO_KEY')
});
// Calling it twice yields same result (using the key does not create it)
common.expectsError(
() => new errors.Error('TEST_FOO_KEY'),
{
code: 'ERR_ASSERTION',
message: invalidKey('TEST_FOO_KEY')
});
common.expectsError(
() => new errors.Error(1),
{
code: 'ERR_ASSERTION',
message: invalidKey(1)
});
common.expectsError(
() => new errors.Error({}),
{
code: 'ERR_ASSERTION',
message: invalidKey('\\[object Object\\]')
});
common.expectsError(
() => new errors.Error([]),
{
code: 'ERR_ASSERTION',
message: invalidKey('')
});
common.expectsError(
() => new errors.Error(true),
{
code: 'ERR_ASSERTION',
message: invalidKey('true')
});
common.expectsError(
() => new errors.TypeError(1),
{
code: 'ERR_ASSERTION',
message: invalidKey(1)
});
common.expectsError(
() => new errors.TypeError({}),
{
code: 'ERR_ASSERTION',
message: invalidKey('\\[object Object\\]')
});
common.expectsError(
() => new errors.TypeError([]),
{
code: 'ERR_ASSERTION',
message: invalidKey('')
});
common.expectsError(
() => new errors.TypeError(true),
{
code: 'ERR_ASSERTION',
message: invalidKey('true')
});
common.expectsError(
() => new errors.RangeError(1),
{
code: 'ERR_ASSERTION',
message: invalidKey(1)
});
common.expectsError(
() => new errors.RangeError({}),
{
code: 'ERR_ASSERTION',
message: invalidKey('\\[object Object\\]')
});
common.expectsError(
() => new errors.RangeError([]),
{
code: 'ERR_ASSERTION',
message: invalidKey('')
});
common.expectsError(
() => new errors.RangeError(true),
{
code: 'ERR_ASSERTION',
message: invalidKey('true')
});
// Tests for common.expectsError // Tests for common.expectsError
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1' }); }, { code: 'TEST_ERROR_1' });
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1', }, { code: 'TEST_ERROR_1',
type: TypeError, type: TypeError,
message: /^Error for testing/ }); message: /^Error for testing/ });
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1', type: TypeError }); }, { code: 'TEST_ERROR_1', type: TypeError });
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { }, {
code: 'TEST_ERROR_1', code: 'TEST_ERROR_1',
type: TypeError, type: TypeError,
@ -160,7 +71,7 @@ common.expectsError(() => {
common.expectsError(() => { common.expectsError(() => {
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1', type: RangeError }); }, { code: 'TEST_ERROR_1', type: RangeError });
}, { }, {
code: 'ERR_ASSERTION', code: 'ERR_ASSERTION',
@ -169,7 +80,7 @@ common.expectsError(() => {
common.expectsError(() => { common.expectsError(() => {
common.expectsError(() => { common.expectsError(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a'); throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1', }, { code: 'TEST_ERROR_1',
type: TypeError, type: TypeError,
message: /^Error for testing 2/ }); message: /^Error for testing 2/ });
@ -318,7 +229,7 @@ assert.strictEqual(
{ {
const { kMaxLength } = process.binding('buffer'); const { kMaxLength } = process.binding('buffer');
const error = new errors.Error('ERR_BUFFER_TOO_LARGE'); const error = new errors.codes.ERR_BUFFER_TOO_LARGE();
assert.strictEqual( assert.strictEqual(
error.message, error.message,
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes` `Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`
@ -326,7 +237,7 @@ assert.strictEqual(
} }
{ {
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', '\u0000bar'); const error = new errors.codes.ERR_INVALID_ARG_VALUE('foo', '\u0000bar');
assert.strictEqual( assert.strictEqual(
error.message, error.message,
'The argument \'foo\' is invalid. Received \'\\u0000bar\'' 'The argument \'foo\' is invalid. Received \'\\u0000bar\''
@ -334,9 +245,9 @@ assert.strictEqual(
} }
{ {
const error = new errors.Error( const error = new errors.codes.ERR_INVALID_ARG_VALUE(
'ERR_INVALID_ARG_VALUE', 'foo', { a: 1 }, 'must have property \'b\''
'foo', { a: 1 }, 'must have property \'b\''); );
assert.strictEqual( assert.strictEqual(
error.message, error.message,
'The argument \'foo\' must have property \'b\'. Received { a: 1 }' 'The argument \'foo\' must have property \'b\'. Received { a: 1 }'
@ -346,7 +257,7 @@ assert.strictEqual(
// Test that `code` property is mutable and that changing it does not change the // Test that `code` property is mutable and that changing it does not change the
// name. // name.
{ {
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'); const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
assert.strictEqual(myError.code, 'ERR_TLS_HANDSHAKE_TIMEOUT'); assert.strictEqual(myError.code, 'ERR_TLS_HANDSHAKE_TIMEOUT');
assert.strictEqual(myError.hasOwnProperty('code'), false); assert.strictEqual(myError.hasOwnProperty('code'), false);
assert.strictEqual(myError.hasOwnProperty('name'), false); assert.strictEqual(myError.hasOwnProperty('name'), false);
@ -364,7 +275,7 @@ assert.strictEqual(
// `console.log()` results, which is the behavior of `Error` objects in the // `console.log()` results, which is the behavior of `Error` objects in the
// browser. Note that `name` becomes enumerable after being assigned. // browser. Note that `name` becomes enumerable after being assigned.
{ {
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'); const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
assert.deepStrictEqual(Object.keys(myError), []); assert.deepStrictEqual(Object.keys(myError), []);
const initialToString = myError.toString(); const initialToString = myError.toString();
@ -379,7 +290,7 @@ assert.strictEqual(
{ {
let initialConsoleLog = ''; let initialConsoleLog = '';
common.hijackStdout((data) => { initialConsoleLog += data; }); common.hijackStdout((data) => { initialConsoleLog += data; });
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'); const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
assert.deepStrictEqual(Object.keys(myError), []); assert.deepStrictEqual(Object.keys(myError), []);
const initialToString = myError.toString(); const initialToString = myError.toString();
console.log(myError); console.log(myError);

View File

@ -170,7 +170,7 @@ util.error('test');
assert.strictEqual(util.types.isNativeError(Object.create(Error.prototype)), assert.strictEqual(util.types.isNativeError(Object.create(Error.prototype)),
false); false);
assert.strictEqual( assert.strictEqual(
util.types.isNativeError(new errors.Error('ERR_IPC_CHANNEL_CLOSED')), util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()),
true true
); );
} }