Skip to content

Commit 49963f4

Browse files
committed
lib: remove unused internal error constructors
PR-URL: #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>
1 parent a910320 commit 49963f4

File tree

4 files changed

+32
-123
lines changed

4 files changed

+32
-123
lines changed

lib/fs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const fs = exports;
3636
const { Buffer } = require('buffer');
3737
const errors = require('internal/errors');
3838
const {
39+
ERR_FS_WATCHER_ALREADY_STARTED,
40+
ERR_FS_WATCHER_NOT_STARTED,
3941
ERR_INVALID_ARG_TYPE,
4042
ERR_INVALID_CALLBACK,
4143
ERR_OUT_OF_RANGE
@@ -1349,7 +1351,7 @@ FSWatcher.prototype.start = function(filename,
13491351
encoding) {
13501352
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
13511353
if (this._handle.initialized) {
1352-
throw new errors.Error('ERR_FS_WATCHER_ALREADY_STARTED');
1354+
throw new ERR_FS_WATCHER_ALREADY_STARTED();
13531355
}
13541356

13551357
filename = getPathFromURL(filename);
@@ -1373,7 +1375,7 @@ FSWatcher.prototype.start = function(filename,
13731375
FSWatcher.prototype.close = function() {
13741376
lazyAssert()(this._handle instanceof FSEvent, 'handle must be a FSEvent');
13751377
if (!this._handle.initialized) {
1376-
throw new errors.Error('ERR_FS_WATCHER_NOT_STARTED');
1378+
throw new ERR_FS_WATCHER_NOT_STARTED();
13771379
}
13781380
this._handle.close();
13791381
};

lib/internal/errors.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ function makeNodeErrorWithCode(Base, key) {
125125
// *only* to allow for testing.
126126
function E(sym, val, def, ...otherClasses) {
127127
messages.set(sym, val);
128-
if (def === undefined) return;
129128
def = makeNodeErrorWithCode(def, sym);
130129
if (otherClasses.length !== 0) {
131130
otherClasses.forEach((clazz) => {
@@ -572,23 +571,20 @@ module.exports = exports = {
572571
exceptionWithHostPort,
573572
uvException,
574573
message,
575-
Error: makeNodeError(Error),
576-
TypeError: makeNodeError(TypeError),
577-
RangeError: makeNodeError(RangeError),
578-
URIError: makeNodeError(URIError),
579574
AssertionError,
580575
SystemError,
581576
codes,
582577
E, // This is exported only to facilitate testing.
583578
errorCache: new Map() // This is in here only to facilitate testing.
584579
};
585580

586-
// To declare an error message, use the E(sym, val) function above. The sym
581+
// To declare an error message, use the E(sym, val, def) function above. The sym
587582
// must be an upper case string. The val can be either a function or a string.
583+
// The def must be an error class.
588584
// The return value of the function must be a string.
589585
// Examples:
590-
// E('EXAMPLE_KEY1', 'This is the error value');
591-
// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`);
586+
// E('EXAMPLE_KEY1', 'This is the error value', Error);
587+
// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`, RangeError);
592588
//
593589
// Once an error code has been assigned, the code itself MUST NOT change and
594590
// any given error code must never be reused to identify a different error.
@@ -858,7 +854,6 @@ E('ERR_STREAM_UNSHIFT_AFTER_END_EVENT',
858854
'stream.unshift() after end event', Error);
859855
E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
860856
E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error);
861-
E('ERR_SYSTEM_ERROR', sysError);
862857
E('ERR_TLS_CERT_ALTNAME_INVALID',
863858
'Hostname/IP does not match certificate\'s altnames: %s', Error);
864859
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,
934929
}
935930
return message;
936931
}
932+
messages.set('ERR_SYSTEM_ERROR', sysError);
937933

938934
function invalidArgType(name, expected, actual) {
939935
internalAssert(name, 'name is required');

test/parallel/test-internal-errors.js

Lines changed: 22 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,153 +5,64 @@ const common = require('../common');
55
const assert = require('assert');
66
const errors = require('internal/errors');
77

8-
function invalidKey(key) {
9-
return new RegExp(`^An invalid error message key was used: ${key}\\.$`);
10-
}
11-
12-
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s');
13-
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
8+
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
9+
Error, TypeError, RangeError);
10+
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
1411

1512
{
16-
const err = new errors.Error('TEST_ERROR_1', 'test');
13+
const err = new errors.codes.TEST_ERROR_1('test');
1714
assert(err instanceof Error);
1815
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
1916
assert.strictEqual(err.message, 'Error for testing purposes: test');
2017
assert.strictEqual(err.code, 'TEST_ERROR_1');
2118
}
2219

2320
{
24-
const err = new errors.TypeError('TEST_ERROR_1', 'test');
21+
const err = new errors.codes.TEST_ERROR_1.TypeError('test');
2522
assert(err instanceof TypeError);
2623
assert.strictEqual(err.name, 'TypeError [TEST_ERROR_1]');
2724
assert.strictEqual(err.message, 'Error for testing purposes: test');
2825
assert.strictEqual(err.code, 'TEST_ERROR_1');
2926
}
3027

3128
{
32-
const err = new errors.RangeError('TEST_ERROR_1', 'test');
29+
const err = new errors.codes.TEST_ERROR_1.RangeError('test');
3330
assert(err instanceof RangeError);
3431
assert.strictEqual(err.name, 'RangeError [TEST_ERROR_1]');
3532
assert.strictEqual(err.message, 'Error for testing purposes: test');
3633
assert.strictEqual(err.code, 'TEST_ERROR_1');
3734
}
3835

3936
{
40-
const err = new errors.Error('TEST_ERROR_2', 'abc', 'xyz');
37+
const err = new errors.codes.TEST_ERROR_2('abc', 'xyz');
4138
assert(err instanceof Error);
4239
assert.strictEqual(err.name, 'Error [TEST_ERROR_2]');
4340
assert.strictEqual(err.message, 'abc xyz');
4441
assert.strictEqual(err.code, 'TEST_ERROR_2');
4542
}
4643

4744
{
48-
const err = new errors.Error('TEST_ERROR_1');
45+
const err = new errors.codes.TEST_ERROR_1();
4946
assert(err instanceof Error);
5047
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
5148
assert.strictEqual(err.message, 'Error for testing purposes: %s');
5249
assert.strictEqual(err.code, 'TEST_ERROR_1');
5350
}
5451

55-
common.expectsError(
56-
() => new errors.Error('TEST_FOO_KEY'),
57-
{
58-
code: 'ERR_ASSERTION',
59-
message: invalidKey('TEST_FOO_KEY')
60-
});
61-
// Calling it twice yields same result (using the key does not create it)
62-
common.expectsError(
63-
() => new errors.Error('TEST_FOO_KEY'),
64-
{
65-
code: 'ERR_ASSERTION',
66-
message: invalidKey('TEST_FOO_KEY')
67-
});
68-
common.expectsError(
69-
() => new errors.Error(1),
70-
{
71-
code: 'ERR_ASSERTION',
72-
message: invalidKey(1)
73-
});
74-
common.expectsError(
75-
() => new errors.Error({}),
76-
{
77-
code: 'ERR_ASSERTION',
78-
message: invalidKey('\\[object Object\\]')
79-
});
80-
common.expectsError(
81-
() => new errors.Error([]),
82-
{
83-
code: 'ERR_ASSERTION',
84-
message: invalidKey('')
85-
});
86-
common.expectsError(
87-
() => new errors.Error(true),
88-
{
89-
code: 'ERR_ASSERTION',
90-
message: invalidKey('true')
91-
});
92-
common.expectsError(
93-
() => new errors.TypeError(1),
94-
{
95-
code: 'ERR_ASSERTION',
96-
message: invalidKey(1)
97-
});
98-
common.expectsError(
99-
() => new errors.TypeError({}),
100-
{
101-
code: 'ERR_ASSERTION',
102-
message: invalidKey('\\[object Object\\]')
103-
});
104-
common.expectsError(
105-
() => new errors.TypeError([]),
106-
{
107-
code: 'ERR_ASSERTION',
108-
message: invalidKey('')
109-
});
110-
common.expectsError(
111-
() => new errors.TypeError(true),
112-
{
113-
code: 'ERR_ASSERTION',
114-
message: invalidKey('true')
115-
});
116-
common.expectsError(
117-
() => new errors.RangeError(1),
118-
{
119-
code: 'ERR_ASSERTION',
120-
message: invalidKey(1)
121-
});
122-
common.expectsError(
123-
() => new errors.RangeError({}),
124-
{
125-
code: 'ERR_ASSERTION',
126-
message: invalidKey('\\[object Object\\]')
127-
});
128-
common.expectsError(
129-
() => new errors.RangeError([]),
130-
{
131-
code: 'ERR_ASSERTION',
132-
message: invalidKey('')
133-
});
134-
common.expectsError(
135-
() => new errors.RangeError(true),
136-
{
137-
code: 'ERR_ASSERTION',
138-
message: invalidKey('true')
139-
});
140-
14152
// Tests for common.expectsError
14253
common.expectsError(() => {
143-
throw new errors.TypeError('TEST_ERROR_1', 'a');
54+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
14455
}, { code: 'TEST_ERROR_1' });
14556
common.expectsError(() => {
146-
throw new errors.TypeError('TEST_ERROR_1', 'a');
57+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
14758
}, { code: 'TEST_ERROR_1',
14859
type: TypeError,
14960
message: /^Error for testing/ });
15061
common.expectsError(() => {
151-
throw new errors.TypeError('TEST_ERROR_1', 'a');
62+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
15263
}, { code: 'TEST_ERROR_1', type: TypeError });
15364
common.expectsError(() => {
154-
throw new errors.TypeError('TEST_ERROR_1', 'a');
65+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
15566
}, {
15667
code: 'TEST_ERROR_1',
15768
type: TypeError,
@@ -160,7 +71,7 @@ common.expectsError(() => {
16071

16172
common.expectsError(() => {
16273
common.expectsError(() => {
163-
throw new errors.TypeError('TEST_ERROR_1', 'a');
74+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
16475
}, { code: 'TEST_ERROR_1', type: RangeError });
16576
}, {
16677
code: 'ERR_ASSERTION',
@@ -169,7 +80,7 @@ common.expectsError(() => {
16980

17081
common.expectsError(() => {
17182
common.expectsError(() => {
172-
throw new errors.TypeError('TEST_ERROR_1', 'a');
83+
throw new errors.codes.TEST_ERROR_1.TypeError('a');
17384
}, { code: 'TEST_ERROR_1',
17485
type: TypeError,
17586
message: /^Error for testing 2/ });
@@ -318,25 +229,25 @@ assert.strictEqual(
318229

319230
{
320231
const { kMaxLength } = process.binding('buffer');
321-
const error = new errors.Error('ERR_BUFFER_TOO_LARGE');
232+
const error = new errors.codes.ERR_BUFFER_TOO_LARGE();
322233
assert.strictEqual(
323234
error.message,
324235
`Cannot create a Buffer larger than 0x${kMaxLength.toString(16)} bytes`
325236
);
326237
}
327238

328239
{
329-
const error = new errors.Error('ERR_INVALID_ARG_VALUE', 'foo', '\u0000bar');
240+
const error = new errors.codes.ERR_INVALID_ARG_VALUE('foo', '\u0000bar');
330241
assert.strictEqual(
331242
error.message,
332243
'The argument \'foo\' is invalid. Received \'\\u0000bar\''
333244
);
334245
}
335246

336247
{
337-
const error = new errors.Error(
338-
'ERR_INVALID_ARG_VALUE',
339-
'foo', { a: 1 }, 'must have property \'b\'');
248+
const error = new errors.codes.ERR_INVALID_ARG_VALUE(
249+
'foo', { a: 1 }, 'must have property \'b\''
250+
);
340251
assert.strictEqual(
341252
error.message,
342253
'The argument \'foo\' must have property \'b\'. Received { a: 1 }'
@@ -346,7 +257,7 @@ assert.strictEqual(
346257
// Test that `code` property is mutable and that changing it does not change the
347258
// name.
348259
{
349-
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT');
260+
const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
350261
assert.strictEqual(myError.code, 'ERR_TLS_HANDSHAKE_TIMEOUT');
351262
assert.strictEqual(myError.hasOwnProperty('code'), false);
352263
assert.strictEqual(myError.hasOwnProperty('name'), false);
@@ -364,7 +275,7 @@ assert.strictEqual(
364275
// `console.log()` results, which is the behavior of `Error` objects in the
365276
// browser. Note that `name` becomes enumerable after being assigned.
366277
{
367-
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT');
278+
const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
368279
assert.deepStrictEqual(Object.keys(myError), []);
369280
const initialToString = myError.toString();
370281

@@ -379,7 +290,7 @@ assert.strictEqual(
379290
{
380291
let initialConsoleLog = '';
381292
common.hijackStdout((data) => { initialConsoleLog += data; });
382-
const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT');
293+
const myError = new errors.codes.ERR_TLS_HANDSHAKE_TIMEOUT();
383294
assert.deepStrictEqual(Object.keys(myError), []);
384295
const initialToString = myError.toString();
385296
console.log(myError);

test/parallel/test-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ util.error('test');
170170
assert.strictEqual(util.types.isNativeError(Object.create(Error.prototype)),
171171
false);
172172
assert.strictEqual(
173-
util.types.isNativeError(new errors.Error('ERR_IPC_CHANNEL_CLOSED')),
173+
util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()),
174174
true
175175
);
176176
}

0 commit comments

Comments
 (0)