Skip to content

Commit 20285ad

Browse files
micnicsilverwind
authored andcommitted
lib: Consistent error messages in all modules
This commit fixes some error messages that are not consistent with some general rules which most of the error messages follow. PR-URL: #3374 Reviewed-By: Roman Reiss <[email protected]>
1 parent af46112 commit 20285ad

25 files changed

+110
-106
lines changed

lib/_http_client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ function ClientRequest(options, cb) {
5151
// well, and b) possibly too restrictive for real-world usage. That's
5252
// why it only scans for spaces because those are guaranteed to create
5353
// an invalid request.
54-
throw new TypeError('Request path contains unescaped characters.');
54+
throw new TypeError('Request path contains unescaped characters');
5555
} else if (protocol !== expectedProtocol) {
5656
throw new Error('Protocol "' + protocol + '" not supported. ' +
57-
'Expected "' + expectedProtocol + '".');
57+
'Expected "' + expectedProtocol + '"');
5858
}
5959

6060
const defaultPort = options.defaultPort ||

lib/_http_outgoing.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,11 @@ OutgoingMessage.prototype.setHeader = function(name, value) {
337337
throw new TypeError(
338338
'Header name must be a valid HTTP Token ["' + name + '"]');
339339
if (typeof name !== 'string')
340-
throw new TypeError('`name` should be a string in setHeader(name, value).');
340+
throw new TypeError('"name" should be a string in setHeader(name, value)');
341341
if (value === undefined)
342-
throw new Error('`value` required in setHeader("' + name + '", value).');
342+
throw new Error('"value" required in setHeader("' + name + '", value)');
343343
if (this._header)
344-
throw new Error('Can\'t set headers after they are sent.');
344+
throw new Error('Can\'t set headers after they are sent');
345345

346346
if (this._headers === null)
347347
this._headers = {};
@@ -357,7 +357,7 @@ OutgoingMessage.prototype.setHeader = function(name, value) {
357357

358358
OutgoingMessage.prototype.getHeader = function(name) {
359359
if (arguments.length < 1) {
360-
throw new Error('`name` is required for getHeader(name).');
360+
throw new Error('"name" argument is required for getHeader(name)');
361361
}
362362

363363
if (!this._headers) return;
@@ -369,11 +369,11 @@ OutgoingMessage.prototype.getHeader = function(name) {
369369

370370
OutgoingMessage.prototype.removeHeader = function(name) {
371371
if (arguments.length < 1) {
372-
throw new Error('`name` is required for removeHeader(name).');
372+
throw new Error('"name" argument is required for removeHeader(name)');
373373
}
374374

375375
if (this._header) {
376-
throw new Error('Can\'t remove headers after they are sent.');
376+
throw new Error('Can\'t remove headers after they are sent');
377377
}
378378

379379
var key = name.toLowerCase();
@@ -392,7 +392,7 @@ OutgoingMessage.prototype.removeHeader = function(name) {
392392

393393
OutgoingMessage.prototype._renderHeaders = function() {
394394
if (this._header) {
395-
throw new Error('Can\'t render headers after they are sent to the client.');
395+
throw new Error('Can\'t render headers after they are sent to the client');
396396
}
397397

398398
var headersMap = this._headers;
@@ -436,7 +436,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
436436
}
437437

438438
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
439-
throw new TypeError('first argument must be a string or Buffer');
439+
throw new TypeError('First argument must be a string or Buffer');
440440
}
441441

442442

@@ -533,7 +533,7 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) {
533533
}
534534

535535
if (data && typeof data !== 'string' && !(data instanceof Buffer)) {
536-
throw new TypeError('first argument must be a string or Buffer');
536+
throw new TypeError('First argument must be a string or Buffer');
537537
}
538538

539539
if (this.finished) {

lib/_stream_readable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ function endReadable(stream) {
889889
// If we get here before consuming all the bytes, then that is a
890890
// bug in node. Should never happen.
891891
if (state.length > 0)
892-
throw new Error('endReadable called on non-empty stream');
892+
throw new Error('"endReadable()" called on non-empty stream');
893893

894894
if (!state.endEmitted) {
895895
state.ended = true;

lib/_stream_transform.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Transform.prototype.push = function(chunk, encoding) {
139139
// an error, then that'll put the hurt on the whole operation. If you
140140
// never call cb(), then you'll never get another chunk.
141141
Transform.prototype._transform = function(chunk, encoding, cb) {
142-
throw new Error('not implemented');
142+
throw new Error('Not implemented');
143143
};
144144

145145
Transform.prototype._write = function(chunk, encoding, cb) {
@@ -183,10 +183,10 @@ function done(stream, er) {
183183
var ts = stream._transformState;
184184

185185
if (ws.length)
186-
throw new Error('calling transform done when ws.length != 0');
186+
throw new Error('Calling transform done when ws.length != 0');
187187

188188
if (ts.transforming)
189-
throw new Error('calling transform done when still transforming');
189+
throw new Error('Calling transform done when still transforming');
190190

191191
return stream.push(null);
192192
}

lib/_stream_writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function Writable(options) {
151151

152152
// Otherwise people can pipe Writable streams, which is just wrong.
153153
Writable.prototype.pipe = function() {
154-
this.emit('error', new Error('Cannot pipe. Not readable.'));
154+
this.emit('error', new Error('Cannot pipe, not readable'));
155155
};
156156

157157

lib/_tls_legacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var Connection = null;
1313
try {
1414
Connection = process.binding('crypto').Connection;
1515
} catch (e) {
16-
throw new Error('node.js not compiled with openssl crypto support.');
16+
throw new Error('Node.js is not compiled with openssl crypto support');
1717
}
1818

1919
function SlabBuffer() {
@@ -590,7 +590,7 @@ function onhandshakestart() {
590590
// state machine and OpenSSL is not re-entrant. We cannot allow the user's
591591
// callback to destroy the connection right now, it would crash and burn.
592592
setImmediate(function() {
593-
var err = new Error('TLS session renegotiation attack detected.');
593+
var err = new Error('TLS session renegotiation attack detected');
594594
if (self.cleartext) self.cleartext.emit('error', err);
595595
});
596596
}

lib/_tls_wrap.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function onhandshakestart() {
3737
// state machine and OpenSSL is not re-entrant. We cannot allow the user's
3838
// callback to destroy the connection right now, it would crash and burn.
3939
setImmediate(function() {
40-
var err = new Error('TLS session renegotiation attack detected.');
40+
var err = new Error('TLS session renegotiation attack detected');
4141
self._emitTLSError(err);
4242
});
4343
}
@@ -756,7 +756,7 @@ function Server(/* [options], listener */) {
756756
var timeout = options.handshakeTimeout || (120 * 1000);
757757

758758
if (typeof timeout !== 'number') {
759-
throw new TypeError('handshakeTimeout must be a number');
759+
throw new TypeError('"handshakeTimeout" option must be a number');
760760
}
761761

762762
if (self.sessionTimeout) {
@@ -902,7 +902,7 @@ Server.prototype.setOptions = function(options) {
902902
// SNI Contexts High-Level API
903903
Server.prototype.addContext = function(servername, context) {
904904
if (!servername) {
905-
throw new Error('Servername is required parameter for Server.addContext');
905+
throw new Error('"servername" is required parameter for Server.addContext');
906906
}
907907

908908
var re = new RegExp('^' +

lib/assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ function _throws(shouldThrow, block, expected, message) {
285285
var actual;
286286

287287
if (typeof block !== 'function') {
288-
throw new TypeError('block must be a function');
288+
throw new TypeError('"block" argument must be a function');
289289
}
290290

291291
if (typeof expected === 'string') {

lib/buffer.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function fromObject(obj) {
136136
}
137137

138138
if (obj == null) {
139-
throw new TypeError('must start with number, buffer, array or string');
139+
throw new TypeError('Must start with number, buffer, array or string');
140140
}
141141

142142
if (obj instanceof ArrayBuffer) {
@@ -164,7 +164,7 @@ function fromObject(obj) {
164164
return b;
165165
}
166166

167-
throw new TypeError('must start with number, buffer, array or string');
167+
throw new TypeError('Must start with number, buffer, array or string');
168168
}
169169

170170

@@ -217,7 +217,7 @@ Buffer.isEncoding = function(encoding) {
217217

218218
Buffer.concat = function(list, length) {
219219
if (!Array.isArray(list))
220-
throw new TypeError('list argument must be an Array of Buffers.');
220+
throw new TypeError('"list" argument must be an Array of Buffers');
221221

222222
if (list.length === 0)
223223
return new Buffer(0);
@@ -375,7 +375,7 @@ Buffer.prototype.toString = function() {
375375
var result = slowToString.apply(this, arguments);
376376
}
377377
if (result === undefined)
378-
throw new Error('toString failed');
378+
throw new Error('"toString()" failed');
379379
return result;
380380
};
381381

@@ -462,7 +462,7 @@ Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
462462
return binding.indexOfNumber(this, val, byteOffset);
463463
}
464464

465-
throw new TypeError('val must be string, number or Buffer');
465+
throw new TypeError('"val" argument must be string, number or Buffer');
466466
};
467467

468468

@@ -471,7 +471,7 @@ Buffer.prototype.fill = function fill(val, start, end) {
471471
end = (end === undefined) ? this.length : end >> 0;
472472

473473
if (start < 0 || end > this.length)
474-
throw new RangeError('out of range index');
474+
throw new RangeError('Out of range index');
475475
if (end <= start)
476476
return this;
477477

@@ -493,7 +493,7 @@ Buffer.prototype.fill = function fill(val, start, end) {
493493
Buffer.prototype.get = internalUtil.deprecate(function get(offset) {
494494
offset = ~~offset;
495495
if (offset < 0 || offset >= this.length)
496-
throw new RangeError('index out of range');
496+
throw new RangeError('Index out of range');
497497
return this[offset];
498498
}, 'Buffer.get is deprecated. Use array indexes instead.');
499499

@@ -502,7 +502,7 @@ Buffer.prototype.get = internalUtil.deprecate(function get(offset) {
502502
Buffer.prototype.set = internalUtil.deprecate(function set(offset, v) {
503503
offset = ~~offset;
504504
if (offset < 0 || offset >= this.length)
505-
throw new RangeError('index out of range');
505+
throw new RangeError('Index out of range');
506506
return this[offset] = v;
507507
}, 'Buffer.set is deprecated. Use array indexes instead.');
508508

@@ -552,7 +552,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
552552
length = remaining;
553553

554554
if (string.length > 0 && (length < 0 || offset < 0))
555-
throw new RangeError('attempt to write outside buffer bounds');
555+
throw new RangeError('Attempt to write outside buffer bounds');
556556

557557
if (!encoding)
558558
encoding = 'utf8';
@@ -612,7 +612,7 @@ Buffer.prototype.slice = function slice(start, end) {
612612

613613
function checkOffset(offset, ext, length) {
614614
if (offset + ext > length)
615-
throw new RangeError('index out of range');
615+
throw new RangeError('Index out of range');
616616
}
617617

618618

@@ -820,11 +820,11 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
820820

821821
function checkInt(buffer, value, offset, ext, max, min) {
822822
if (!(buffer instanceof Buffer))
823-
throw new TypeError('buffer must be a Buffer instance');
823+
throw new TypeError('"buffer" argument must be a Buffer instance');
824824
if (value > max || value < min)
825-
throw new TypeError('value is out of bounds');
825+
throw new TypeError('"value" argument is out of bounds');
826826
if (offset + ext > buffer.length)
827-
throw new RangeError('index out of range');
827+
throw new RangeError('Index out of range');
828828
}
829829

830830

@@ -1030,9 +1030,9 @@ Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
10301030

10311031
function checkFloat(buffer, value, offset, ext) {
10321032
if (!(buffer instanceof Buffer))
1033-
throw new TypeError('buffer must be a Buffer instance');
1033+
throw new TypeError('"buffer" argument must be a Buffer instance');
10341034
if (offset + ext > buffer.length)
1035-
throw new RangeError('index out of range');
1035+
throw new RangeError('Index out of range');
10361036
}
10371037

10381038

lib/child_process.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
251251
stdoutLen += chunk.length;
252252

253253
if (stdoutLen > options.maxBuffer) {
254-
ex = new Error('stdout maxBuffer exceeded.');
254+
ex = new Error('stdout maxBuffer exceeded');
255255
kill();
256256
} else {
257257
if (!encoding)
@@ -265,7 +265,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
265265
stderrLen += chunk.length;
266266

267267
if (stderrLen > options.maxBuffer) {
268-
ex = new Error('stderr maxBuffer exceeded.');
268+
ex = new Error('stderr maxBuffer exceeded');
269269
kill();
270270
} else {
271271
if (!encoding)
@@ -316,7 +316,7 @@ function normalizeSpawnArguments(file /*, args, options*/) {
316316
if (options === undefined)
317317
options = {};
318318
else if (options === null || typeof options !== 'object')
319-
throw new TypeError('options argument must be an object');
319+
throw new TypeError('"options" argument must be an object');
320320

321321
options = util._extend({}, options);
322322
args.unshift(file);

lib/crypto.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ try {
1212
var getHashes = binding.getHashes;
1313
var getCurves = binding.getCurves;
1414
} catch (e) {
15-
throw new Error('node.js not compiled with openssl crypto support.');
15+
throw new Error('Node.js is not compiled with openssl crypto support');
1616
}
1717

1818
const Buffer = require('buffer').Buffer;
@@ -486,7 +486,7 @@ DiffieHellman.prototype.setPrivateKey = function(key, encoding) {
486486

487487
function ECDH(curve) {
488488
if (typeof curve !== 'string')
489-
throw new TypeError('curve should be a string');
489+
throw new TypeError('"curve" argument should be a string');
490490

491491
this._handle = new binding.ECDH(curve);
492492
}
@@ -604,10 +604,10 @@ Certificate.prototype.exportChallenge = function(object, encoding) {
604604

605605
exports.setEngine = function setEngine(id, flags) {
606606
if (typeof id !== 'string')
607-
throw new TypeError('id should be a string');
607+
throw new TypeError('"id" argument should be a string');
608608

609609
if (flags && typeof flags !== 'number')
610-
throw new TypeError('flags should be a number, if present');
610+
throw new TypeError('"flags" argument should be a number, if present');
611611
flags = flags >>> 0;
612612

613613
// Use provided engine for everything by default

lib/dgram.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function newHandle(type) {
5454
}
5555

5656
if (type == 'unix_dgram')
57-
throw new Error('unix_dgram sockets are not supported any more.');
57+
throw new Error('"unix_dgram" type sockets are not supported any more');
5858

5959
throw new Error('Bad socket type specified. Valid types are: udp4, udp6');
6060
}
@@ -233,7 +233,7 @@ Socket.prototype.sendto = function(buffer,
233233
address,
234234
callback) {
235235
if (typeof offset !== 'number' || typeof length !== 'number')
236-
throw new Error('send takes offset and length as args 2 and 3');
236+
throw new Error('Send takes "offset" and "length" as args 2 and 3');
237237

238238
if (typeof address !== 'string')
239239
throw new Error(this.type + ' sockets must send to port, address');
@@ -254,15 +254,15 @@ Socket.prototype.send = function(buffer,
254254
buffer = new Buffer(buffer);
255255

256256
if (!(buffer instanceof Buffer))
257-
throw new TypeError('First argument must be a buffer or string.');
257+
throw new TypeError('First argument must be a buffer or string');
258258

259259
offset = offset | 0;
260260
if (offset < 0)
261261
throw new RangeError('Offset should be >= 0');
262262

263263
if ((length == 0 && offset > buffer.length) ||
264264
(length > 0 && offset >= buffer.length))
265-
throw new RangeError('Offset into buffer too large');
265+
throw new RangeError('Offset into buffer is too large');
266266

267267
// Sending a zero-length datagram is kind of pointless but it _is_
268268
// allowed, hence check that length >= 0 rather than > 0.

0 commit comments

Comments
 (0)