Skip to content

Commit 1d2fd8b

Browse files
committed
lib: port remaining errors to new system
PR-URL: #19137 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent cb5f9a6 commit 1d2fd8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+761
-615
lines changed

lib/_http_client.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ const { Buffer } = require('buffer');
3939
const { urlToOptions, searchParamsSymbol } = require('internal/url');
4040
const { outHeadersKey, ondrain } = require('internal/http');
4141
const { nextTick } = require('internal/process/next_tick');
42-
const errors = require('internal/errors');
42+
const {
43+
ERR_HTTP_HEADERS_SENT,
44+
ERR_INVALID_ARG_TYPE,
45+
ERR_INVALID_DOMAIN_NAME,
46+
ERR_INVALID_HTTP_TOKEN,
47+
ERR_INVALID_PROTOCOL,
48+
ERR_UNESCAPED_CHARACTERS
49+
} = require('internal/errors').codes;
4350
const { validateTimerDuration } = require('internal/timers');
4451

4552
const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/;
4653

4754
function validateHost(host, name) {
4855
if (host !== null && host !== undefined && typeof host !== 'string') {
49-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', `options.${name}`,
50-
['string', 'undefined', 'null'], host);
56+
throw new ERR_INVALID_ARG_TYPE(`options.${name}`,
57+
['string', 'undefined', 'null'],
58+
host);
5159
}
5260
return host;
5361
}
@@ -58,7 +66,7 @@ function ClientRequest(options, cb) {
5866
if (typeof options === 'string') {
5967
options = url.parse(options);
6068
if (!options.hostname) {
61-
throw new errors.Error('ERR_INVALID_DOMAIN_NAME');
69+
throw new ERR_INVALID_DOMAIN_NAME();
6270
}
6371
} else if (options && options[searchParamsSymbol] &&
6472
options[searchParamsSymbol][searchParamsSymbol]) {
@@ -79,8 +87,8 @@ function ClientRequest(options, cb) {
7987
// Explicitly pass through this statement as agent will not be used
8088
// when createConnection is provided.
8189
} else if (typeof agent.addRequest !== 'function') {
82-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'Agent option',
83-
['Agent-like Object', 'undefined', 'false']);
90+
throw new ERR_INVALID_ARG_TYPE('Agent option',
91+
['Agent-like Object', 'undefined', 'false']);
8492
}
8593
this.agent = agent;
8694

@@ -93,11 +101,11 @@ function ClientRequest(options, cb) {
93101
if (options.path) {
94102
path = String(options.path);
95103
if (INVALID_PATH_REGEX.test(path))
96-
throw new errors.TypeError('ERR_UNESCAPED_CHARACTERS', 'Request path');
104+
throw new ERR_UNESCAPED_CHARACTERS('Request path');
97105
}
98106

99107
if (protocol !== expectedProtocol) {
100-
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
108+
throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol);
101109
}
102110

103111
var defaultPort = options.defaultPort ||
@@ -115,13 +123,12 @@ function ClientRequest(options, cb) {
115123
var method = options.method;
116124
var methodIsString = (typeof method === 'string');
117125
if (method !== null && method !== undefined && !methodIsString) {
118-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
119-
'string', method);
126+
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
120127
}
121128

122129
if (methodIsString && method) {
123130
if (!checkIsHttpToken(method)) {
124-
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Method', method);
131+
throw new ERR_INVALID_HTTP_TOKEN('Method', method);
125132
}
126133
method = this.method = method.toUpperCase();
127134
} else {
@@ -203,7 +210,7 @@ function ClientRequest(options, cb) {
203210

204211
if (this.getHeader('expect')) {
205212
if (this._header) {
206-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
213+
throw new ERR_HTTP_HEADERS_SENT('render');
207214
}
208215

209216
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
@@ -261,7 +268,7 @@ ClientRequest.prototype._finish = function _finish() {
261268

262269
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
263270
if (this._header) {
264-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
271+
throw new ERR_HTTP_HEADERS_SENT('render');
265272
}
266273
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
267274
this[outHeadersKey]);

lib/_http_outgoing.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
3333
const { outHeadersKey } = require('internal/http');
3434
const { async_id_symbol } = require('internal/async_hooks').symbols;
3535
const { nextTick } = require('internal/process/next_tick');
36-
const errors = require('internal/errors');
36+
const {
37+
ERR_HTTP_HEADERS_SENT,
38+
ERR_HTTP_INVALID_HEADER_VALUE,
39+
ERR_HTTP_TRAILER_INVALID,
40+
ERR_INVALID_HTTP_TOKEN,
41+
ERR_INVALID_ARG_TYPE,
42+
ERR_INVALID_CHAR,
43+
ERR_METHOD_NOT_IMPLEMENTED,
44+
ERR_STREAM_CANNOT_PIPE,
45+
ERR_STREAM_WRITE_AFTER_END
46+
} = require('internal/errors').codes;
3747

3848
const { CRLF, debug } = common;
3949
const { utcDate } = internalHttp;
@@ -165,7 +175,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
165175

166176
OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
167177
if (this._header) {
168-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
178+
throw new ERR_HTTP_HEADERS_SENT('render');
169179
}
170180

171181
var headersMap = this[outHeadersKey];
@@ -424,7 +434,7 @@ function _storeHeader(firstLine, headers) {
424434
// header fields, regardless of the header fields present in the
425435
// message, and thus cannot contain a message body or 'trailers'.
426436
if (this.chunkedEncoding !== true && state.trailer) {
427-
throw new errors.Error('ERR_HTTP_TRAILER_INVALID');
437+
throw new ERR_HTTP_TRAILER_INVALID();
428438
}
429439

430440
this._header = state.header + CRLF;
@@ -488,12 +498,12 @@ function matchHeader(self, state, field, value) {
488498
function validateHeader(name, value) {
489499
let err;
490500
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
491-
err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name);
501+
err = new ERR_INVALID_HTTP_TOKEN('Header name', name);
492502
} else if (value === undefined) {
493-
err = new errors.TypeError('ERR_HTTP_INVALID_HEADER_VALUE', value, name);
503+
err = new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
494504
} else if (checkInvalidHeaderChar(value)) {
495505
debug('Header "%s" contains invalid characters', name);
496-
err = new errors.TypeError('ERR_INVALID_CHAR', 'header content', name);
506+
err = new ERR_INVALID_CHAR('header content', name);
497507
}
498508
if (err !== undefined) {
499509
Error.captureStackTrace(err, validateHeader);
@@ -503,7 +513,7 @@ function validateHeader(name, value) {
503513

504514
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
505515
if (this._header) {
506-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'set');
516+
throw new ERR_HTTP_HEADERS_SENT('set');
507517
}
508518
validateHeader(name, value);
509519

@@ -529,7 +539,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
529539

530540
OutgoingMessage.prototype.getHeader = function getHeader(name) {
531541
if (typeof name !== 'string') {
532-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
542+
throw new ERR_INVALID_ARG_TYPE('name', 'string');
533543
}
534544

535545
if (!this[outHeadersKey]) return;
@@ -565,7 +575,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
565575

566576
OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
567577
if (typeof name !== 'string') {
568-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
578+
throw new ERR_INVALID_ARG_TYPE('name', 'string');
569579
}
570580

571581
return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]);
@@ -574,11 +584,11 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
574584

575585
OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
576586
if (typeof name !== 'string') {
577-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
587+
throw new ERR_INVALID_ARG_TYPE('name', 'string');
578588
}
579589

580590
if (this._header) {
581-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'remove');
591+
throw new ERR_HTTP_HEADERS_SENT('remove');
582592
}
583593

584594
var key = name.toLowerCase();
@@ -605,7 +615,7 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
605615

606616

607617
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
608-
throw new errors.Error('ERR_METHOD_NOT_IMPLEMENTED', '_implicitHeader()');
618+
throw new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()');
609619
};
610620

611621
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
@@ -622,7 +632,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
622632

623633
function write_(msg, chunk, encoding, callback, fromEnd) {
624634
if (msg.finished) {
625-
const err = new errors.Error('ERR_STREAM_WRITE_AFTER_END');
635+
const err = new ERR_STREAM_WRITE_AFTER_END();
626636
nextTick(msg.socket && msg.socket[async_id_symbol],
627637
writeAfterEndNT.bind(msg),
628638
err,
@@ -642,8 +652,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
642652
}
643653

644654
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
645-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
646-
['string', 'Buffer']);
655+
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
647656
}
648657

649658

@@ -711,12 +720,11 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
711720
value = headers[key];
712721
}
713722
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
714-
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Trailer name',
715-
field);
723+
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
716724
}
717725
if (checkInvalidHeaderChar(value)) {
718726
debug('Trailer "%s" contains invalid characters', field);
719-
throw new errors.TypeError('ERR_INVALID_CHAR', 'trailer content', field);
727+
throw new ERR_INVALID_CHAR('trailer content', field);
720728
}
721729
this._trailer += field + ': ' + escapeHeaderValue(value) + CRLF;
722730
}
@@ -742,8 +750,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
742750
var uncork;
743751
if (chunk) {
744752
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
745-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
746-
['string', 'Buffer']);
753+
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
747754
}
748755
if (!this._header) {
749756
if (typeof chunk === 'string')
@@ -874,7 +881,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
874881

875882
OutgoingMessage.prototype.pipe = function pipe() {
876883
// OutgoingMessage should be write-only. Piping from it is disabled.
877-
this.emit('error', new errors.Error('ERR_STREAM_CANNOT_PIPE'));
884+
this.emit('error', new ERR_STREAM_CANNOT_PIPE());
878885
};
879886

880887
module.exports = {

lib/_http_server.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ const {
4343
getOrSetAsyncId
4444
} = require('internal/async_hooks');
4545
const { IncomingMessage } = require('_http_incoming');
46-
const errors = require('internal/errors');
46+
const {
47+
ERR_HTTP_HEADERS_SENT,
48+
ERR_HTTP_INVALID_STATUS_CODE,
49+
ERR_INVALID_CHAR
50+
} = require('internal/errors').codes;
4751
const Buffer = require('buffer').Buffer;
4852

4953
const kServerResponse = Symbol('ServerResponse');
@@ -201,8 +205,7 @@ function writeHead(statusCode, reason, obj) {
201205

202206
statusCode |= 0;
203207
if (statusCode < 100 || statusCode > 999) {
204-
throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE',
205-
originalStatusCode);
208+
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
206209
}
207210

208211

@@ -229,7 +232,7 @@ function writeHead(statusCode, reason, obj) {
229232
}
230233
}
231234
if (k === undefined && this._header) {
232-
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
235+
throw new ERR_HTTP_HEADERS_SENT('render');
233236
}
234237
// only progressive api is used
235238
headers = this[outHeadersKey];
@@ -239,7 +242,7 @@ function writeHead(statusCode, reason, obj) {
239242
}
240243

241244
if (checkInvalidHeaderChar(this.statusMessage))
242-
throw new errors.Error('ERR_INVALID_CHAR', 'statusMessage');
245+
throw new ERR_INVALID_CHAR('statusMessage');
243246

244247
var statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}${CRLF}`;
245248

lib/_stream_readable.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ const debug = util.debuglog('stream');
3232
const BufferList = require('internal/streams/BufferList');
3333
const destroyImpl = require('internal/streams/destroy');
3434
const { getHighWaterMark } = require('internal/streams/state');
35-
const errors = require('internal/errors');
35+
const {
36+
ERR_INVALID_ARG_TYPE,
37+
ERR_STREAM_PUSH_AFTER_EOF,
38+
ERR_STREAM_READ_NOT_IMPLEMENTED,
39+
ERR_STREAM_UNSHIFT_AFTER_END_EVENT
40+
} = require('internal/errors').codes;
3641
const ReadableAsyncIterator = require('internal/streams/async_iterator');
3742
const { emitExperimentalWarning } = require('internal/util');
3843
var StringDecoder;
@@ -232,12 +237,11 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
232237

233238
if (addToFront) {
234239
if (state.endEmitted)
235-
stream.emit('error',
236-
new errors.Error('ERR_STREAM_UNSHIFT_AFTER_END_EVENT'));
240+
stream.emit('error', new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
237241
else
238242
addChunk(stream, state, chunk, true);
239243
} else if (state.ended) {
240-
stream.emit('error', new errors.Error('ERR_STREAM_PUSH_AFTER_EOF'));
244+
stream.emit('error', new ERR_STREAM_PUSH_AFTER_EOF());
241245
} else if (state.destroyed) {
242246
return false;
243247
} else {
@@ -285,8 +289,7 @@ function chunkInvalid(state, chunk) {
285289
typeof chunk !== 'string' &&
286290
chunk !== undefined &&
287291
!state.objectMode) {
288-
er = new errors.TypeError('ERR_INVALID_ARG_TYPE',
289-
'chunk', ['string', 'Buffer', 'Uint8Array']);
292+
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array']);
290293
}
291294
return er;
292295
}
@@ -565,7 +568,7 @@ function maybeReadMore_(stream, state) {
565568
// for virtual (non-string, non-buffer) streams, "length" is somewhat
566569
// arbitrary, and perhaps not very meaningful.
567570
Readable.prototype._read = function(n) {
568-
this.emit('error', new errors.Error('ERR_STREAM_READ_NOT_IMPLEMENTED'));
571+
this.emit('error', new ERR_STREAM_READ_NOT_IMPLEMENTED());
569572
};
570573

571574
Readable.prototype.pipe = function(dest, pipeOpts) {

lib/_stream_transform.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@
6464
'use strict';
6565

6666
module.exports = Transform;
67-
const errors = require('internal/errors');
67+
const {
68+
ERR_METHOD_NOT_IMPLEMENTED,
69+
ERR_MULTIPLE_CALLBACK,
70+
ERR_TRANSFORM_ALREADY_TRANSFORMING,
71+
ERR_TRANSFORM_WITH_LENGTH_0
72+
} = require('internal/errors').codes;
6873
const Duplex = require('_stream_duplex');
6974
const util = require('util');
7075
util.inherits(Transform, Duplex);
@@ -77,7 +82,7 @@ function afterTransform(er, data) {
7782
var cb = ts.writecb;
7883

7984
if (cb === null) {
80-
return this.emit('error', new errors.Error('ERR_MULTIPLE_CALLBACK'));
85+
return this.emit('error', new ERR_MULTIPLE_CALLBACK());
8186
}
8287

8388
ts.writechunk = null;
@@ -157,7 +162,7 @@ Transform.prototype.push = function(chunk, encoding) {
157162
// an error, then that'll put the hurt on the whole operation. If you
158163
// never call cb(), then you'll never get another chunk.
159164
Transform.prototype._transform = function(chunk, encoding, cb) {
160-
throw new errors.Error('ERR_METHOD_NOT_IMPLEMENTED', '_transform');
165+
throw new ERR_METHOD_NOT_IMPLEMENTED('_transform');
161166
};
162167

163168
Transform.prototype._write = function(chunk, encoding, cb) {
@@ -209,9 +214,9 @@ function done(stream, er, data) {
209214
// if there's nothing in the write buffer, then that means
210215
// that nothing more will ever be provided
211216
if (stream._writableState.length)
212-
throw new errors.Error('ERR_TRANSFORM_WITH_LENGTH_0');
217+
throw new ERR_TRANSFORM_WITH_LENGTH_0();
213218

214219
if (stream._transformState.transforming)
215-
throw new errors.Error('ERR_TRANSFORM_ALREADY_TRANSFORMING');
220+
throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
216221
return stream.push(null);
217222
}

0 commit comments

Comments
 (0)