Skip to content

Commit 1698c8e

Browse files
BridgeARtniessen
authored andcommitted
errors: fix and improve error types
1) Add missing lazy assert call 2) Remove obsolete error type 3) Name undocumented error type more appropriate 4) Consolidate error type style (rely on util.format instead of using a function) 5) Uppercase the first letter from error messages 6) Improve some internal error parameters PR-URL: #13857 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 8ca9338 commit 1698c8e

9 files changed

+53
-62
lines changed

lib/_http_server.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ function writeHead(statusCode, reason, obj) {
225225
headers = obj;
226226
}
227227

228-
if (common._checkInvalidHeaderChar(this.statusMessage)) {
229-
throw new errors.Error('ERR_HTTP_INVALID_CHAR',
230-
'Invalid character in statusMessage.');
231-
}
228+
if (common._checkInvalidHeaderChar(this.statusMessage))
229+
throw new errors.Error('ERR_INVALID_CHAR', 'statusMessage');
232230

233231
var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;
234232

lib/_stream_transform.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ function afterTransform(er, data) {
7777
var cb = ts.writecb;
7878

7979
if (!cb) {
80-
return this.emit('error',
81-
new errors.Error('ERR_TRANSFORM_MULTIPLE_CALLBACK'));
80+
return this.emit('error', new errors.Error('ERR_MULTIPLE_CALLBACK'));
8281
}
8382

8483
ts.writechunk = null;
@@ -207,6 +206,7 @@ function done(stream, er, data) {
207206
if (data != null) // single equals check for both `null` and `undefined`
208207
stream.push(data);
209208

209+
// TODO(BridgeAR): Write a test for these two error cases
210210
// if there's nothing in the write buffer, then that means
211211
// that nothing more will ever be provided
212212
if (stream._writableState.length)

lib/internal/errors.js

+35-39
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,32 @@ module.exports = exports = {
110110
//
111111
// Note: Please try to keep these in alphabetical order
112112
E('ERR_ARG_NOT_ITERABLE', '%s must be iterable');
113-
E('ERR_ASSERTION', (msg) => msg);
113+
E('ERR_ASSERTION', '%s');
114114
E('ERR_CONSOLE_WRITABLE_STREAM',
115-
(name) => `Console expects a writable stream instance for ${name}`);
116-
E('ERR_CPU_USAGE', (errMsg) => `Unable to obtain cpu usage ${errMsg}`);
115+
'Console expects a writable stream instance for %s');
116+
E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s');
117117
E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value');
118118
E('ERR_HTTP_HEADERS_SENT',
119119
'Cannot render headers after they are sent to the client');
120-
E('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.');
121-
E('ERR_HTTP_INVALID_STATUS_CODE',
122-
(originalStatusCode) => `Invalid status code: ${originalStatusCode}`);
120+
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s');
121+
E('ERR_HTTP_TRAILER_INVALID',
122+
'Trailers are invalid with this transfer encoding');
123123
E('ERR_INDEX_OUT_OF_RANGE', 'Index out of range');
124+
E('ERR_INVALID_ARG_TYPE', invalidArgType);
124125
E('ERR_INVALID_ARRAY_LENGTH',
125126
(name, length, actual) => {
126-
let msg = `The "${name}" array must have a length of ${length}`;
127-
if (arguments.length > 2) {
128-
const len = Array.isArray(actual) ? actual.length : actual;
129-
msg += `. Received length ${len}`;
130-
}
131-
return msg;
127+
const assert = lazyAssert();
128+
assert.strictEqual(typeof actual, 'number');
129+
return `The "${name}" array must have a length of ${
130+
length}. Received length ${actual}`;
132131
});
133-
E('ERR_INVALID_ARG_TYPE', invalidArgType);
134-
E('ERR_INVALID_CALLBACK', 'callback must be a function');
135-
E('ERR_INVALID_FD', (fd) => `"fd" must be a positive integer: ${fd}`);
132+
E('ERR_INVALID_CALLBACK', 'Callback must be a function');
133+
E('ERR_INVALID_CHAR', 'Invalid character in %s');
136134
E('ERR_INVALID_CURSOR_POS',
137135
'Cannot set cursor row without setting its column');
138-
E('ERR_INVALID_FILE_URL_HOST', 'File URL host %s');
136+
E('ERR_INVALID_FD', '"fd" must be a positive integer: %s');
137+
E('ERR_INVALID_FILE_URL_HOST',
138+
'File URL host must be "localhost" or empty on %s');
139139
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s');
140140
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent');
141141
E('ERR_INVALID_OPT_VALUE',
@@ -144,47 +144,42 @@ E('ERR_INVALID_OPT_VALUE',
144144
});
145145
E('ERR_INVALID_REPL_EVAL_CONFIG',
146146
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
147+
E('ERR_INVALID_REPL_HISTORY', 'Expected array, got %s');
147148
E('ERR_INVALID_SYNC_FORK_INPUT',
148-
(value) => {
149-
return 'Asynchronous forks do not support Buffer, Uint8Array or string' +
150-
`input: ${value}`;
151-
});
149+
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
152150
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
153151
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
154152
E('ERR_INVALID_URL', 'Invalid URL: %s');
155153
E('ERR_INVALID_URL_SCHEME',
156-
(expected) => `The URL must be ${oneOf(expected, 'scheme')}`);
157-
E('ERR_INVALID_REPL_HISTORY',
158-
(repl_history) => `Expected array, got ${repl_history}`);
159-
E('ERR_IPC_CHANNEL_CLOSED', 'channel closed');
154+
(expected) => {
155+
lazyAssert();
156+
return `The URL must be ${oneOf(expected, 'scheme')}`;
157+
});
158+
E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed');
160159
E('ERR_IPC_DISCONNECTED', 'IPC channel is already disconnected');
161160
E('ERR_IPC_ONE_PIPE', 'Child process can have only one IPC pipe');
162161
E('ERR_IPC_SYNC_FORK', 'IPC cannot be used with synchronous forks');
163162
E('ERR_MISSING_ARGS', missingArgs);
164-
E('ERR_PARSE_HISTORY_DATA',
165-
(oldHistoryPath) => `Could not parse history data in ${oldHistoryPath}`);
163+
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
166164
E('ERR_NO_CRYPTO', 'Node.js is not compiled with OpenSSL crypto support');
165+
E('ERR_PARSE_HISTORY_DATA', 'Could not parse history data in %s');
166+
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
167+
E('ERR_SOCKET_BAD_TYPE',
168+
'Bad socket type specified. Valid types are: udp4, udp6');
169+
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
170+
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
171+
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
167172
E('ERR_STDERR_CLOSE', 'process.stderr cannot be closed');
168173
E('ERR_STDOUT_CLOSE', 'process.stdout cannot be closed');
169174
E('ERR_STREAM_HAS_STRINGDECODER', 'Stream has StringDecoder');
170175
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
171176
'Calling transform done when still transforming');
172-
E('ERR_TRANSFORM_MULTIPLE_CALLBACK', 'Callback called multiple times');
173177
E('ERR_TRANSFORM_WITH_LENGTH_0',
174-
'Calling transform done when ws.length != 0');
175-
E('ERR_HTTP_TRAILER_INVALID',
176-
'Trailers are invalid with this transfer encoding');
177-
E('ERR_UNKNOWN_BUILTIN_MODULE', (id) => `No such built-in module: ${id}`);
178-
E('ERR_UNKNOWN_SIGNAL', (signal) => `Unknown signal: ${signal}`);
178+
'Calling transform done when writableState.length != 0');
179+
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s');
179180
E('ERR_UNKNOWN_STDIN_TYPE', 'Unknown stdin file type');
180181
E('ERR_UNKNOWN_STREAM_TYPE', 'Unknown stream file type');
181-
E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound');
182-
E('ERR_SOCKET_BAD_TYPE',
183-
'Bad socket type specified. Valid types are: udp4, udp6');
184-
E('ERR_SOCKET_CANNOT_SEND', 'Unable to send data');
185-
E('ERR_SOCKET_BAD_PORT', 'Port should be > 0 and < 65536');
186-
E('ERR_SOCKET_DGRAM_NOT_RUNNING', 'Not running');
187-
E('ERR_V8BREAKITERATOR', 'full ICU data not installed. ' +
182+
E('ERR_V8BREAKITERATOR', 'Full ICU data not installed. ' +
188183
'See https://github.com/nodejs/node/wiki/Intl');
189184
// Add new errors from here...
190185

@@ -200,6 +195,7 @@ function invalidArgType(name, expected, actual) {
200195
}
201196

202197
function missingArgs(...args) {
198+
const assert = lazyAssert();
203199
assert(args.length > 0, 'At least one arg needs to be specified');
204200
let msg = 'The ';
205201
const len = args.length;

lib/internal/process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function setup_hrtime() {
8686
}
8787
if (time.length !== 2) {
8888
throw new errors.TypeError('ERR_INVALID_ARRAY_LENGTH', 'time', 2,
89-
time);
89+
time.length);
9090
}
9191

9292
const sec = (hrValues[0] * 0x100000000 + hrValues[1]) - time[0];

lib/internal/url.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,7 @@ function getPathFromURLWin32(url) {
13521352

13531353
function getPathFromURLPosix(url) {
13541354
if (url.hostname !== '') {
1355-
return new errors.TypeError('ERR_INVALID_FILE_URL_HOST',
1356-
`must be "localhost" or empty on ${platform}`);
1355+
return new errors.TypeError('ERR_INVALID_FILE_URL_HOST', platform);
13571356
}
13581357
var pathname = url.pathname;
13591358
for (var n = 0; n < pathname.length; n++) {

test/parallel/test-child-process-fork-regr-gh-2847.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ const server = net.createServer(function(s) {
6060
send(function(err) {
6161
// Ignore errors when sending the second handle because the worker
6262
// may already have exited.
63-
if (err) {
64-
if ((err.message !== 'channel closed') &&
65-
(err.code !== 'ECONNREFUSED')) {
66-
throw err;
67-
}
63+
if (err && err.message !== 'Channel closed') {
64+
throw err;
6865
}
6966
});
7067
});

test/parallel/test-child-process-send-after-close.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ child.on('close', common.mustCall((code, signal) => {
1010
assert.strictEqual(code, 0);
1111
assert.strictEqual(signal, null);
1212

13-
function testError(err) {
14-
assert.strictEqual(err.message, 'channel closed');
15-
}
13+
const testError = common.expectsError({
14+
type: Error,
15+
message: 'Channel closed',
16+
code: 'ERR_IPC_CHANNEL_CLOSED'
17+
});
1618

1719
child.on('error', common.mustCall(testError));
1820

test/parallel/test-process-next-tick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ process.on('exit', function() {
4343
common.expectsError({
4444
code: 'ERR_INVALID_CALLBACK',
4545
type: TypeError,
46-
message: 'callback must be a function'
46+
message: 'Callback must be a function'
4747
}));
4848
});
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43
const { Transform } = require('stream');
54
const stream = new Transform({
65
transform(chunk, enc, cb) { cb(); cb(); }
76
});
87

9-
stream.on('error', common.mustCall((err) => {
10-
assert.strictEqual(err.toString(),
11-
'Error [ERR_TRANSFORM_MULTIPLE_CALLBACK]: ' +
12-
'Callback called multiple times');
8+
stream.on('error', common.expectsError({
9+
type: Error,
10+
message: 'Callback called multiple times',
11+
code: 'ERR_MULTIPLE_CALLBACK'
1312
}));
1413

1514
stream.write('foo');

0 commit comments

Comments
 (0)