Skip to content

Commit

Permalink
lib: port remaining errors to new system
Browse files Browse the repository at this point in the history
PR-URL: nodejs#19137
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
targos committed Mar 7, 2018
1 parent cb5f9a6 commit 1d2fd8b
Show file tree
Hide file tree
Showing 44 changed files with 761 additions and 615 deletions.
33 changes: 20 additions & 13 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ const { Buffer } = require('buffer');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
const { outHeadersKey, ondrain } = require('internal/http');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_DOMAIN_NAME,
ERR_INVALID_HTTP_TOKEN,
ERR_INVALID_PROTOCOL,
ERR_UNESCAPED_CHARACTERS
} = require('internal/errors').codes;
const { validateTimerDuration } = require('internal/timers');

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

function validateHost(host, name) {
if (host !== null && host !== undefined && typeof host !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', `options.${name}`,
['string', 'undefined', 'null'], host);
throw new ERR_INVALID_ARG_TYPE(`options.${name}`,
['string', 'undefined', 'null'],
host);
}
return host;
}
Expand All @@ -58,7 +66,7 @@ function ClientRequest(options, cb) {
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new errors.Error('ERR_INVALID_DOMAIN_NAME');
throw new ERR_INVALID_DOMAIN_NAME();
}
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
Expand All @@ -79,8 +87,8 @@ function ClientRequest(options, cb) {
// Explicitly pass through this statement as agent will not be used
// when createConnection is provided.
} else if (typeof agent.addRequest !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'Agent option',
['Agent-like Object', 'undefined', 'false']);
throw new ERR_INVALID_ARG_TYPE('Agent option',
['Agent-like Object', 'undefined', 'false']);
}
this.agent = agent;

Expand All @@ -93,11 +101,11 @@ function ClientRequest(options, cb) {
if (options.path) {
path = String(options.path);
if (INVALID_PATH_REGEX.test(path))
throw new errors.TypeError('ERR_UNESCAPED_CHARACTERS', 'Request path');
throw new ERR_UNESCAPED_CHARACTERS('Request path');
}

if (protocol !== expectedProtocol) {
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
throw new ERR_INVALID_PROTOCOL(protocol, expectedProtocol);
}

var defaultPort = options.defaultPort ||
Expand All @@ -115,13 +123,12 @@ function ClientRequest(options, cb) {
var method = options.method;
var methodIsString = (typeof method === 'string');
if (method !== null && method !== undefined && !methodIsString) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
'string', method);
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
}

if (methodIsString && method) {
if (!checkIsHttpToken(method)) {
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Method', method);
throw new ERR_INVALID_HTTP_TOKEN('Method', method);
}
method = this.method = method.toUpperCase();
} else {
Expand Down Expand Up @@ -203,7 +210,7 @@ function ClientRequest(options, cb) {

if (this.getHeader('expect')) {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
throw new ERR_HTTP_HEADERS_SENT('render');
}

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

ClientRequest.prototype._implicitHeader = function _implicitHeader() {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
throw new ERR_HTTP_HEADERS_SENT('render');
}
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
this[outHeadersKey]);
Expand Down
49 changes: 28 additions & 21 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
const { outHeadersKey } = require('internal/http');
const { async_id_symbol } = require('internal/async_hooks').symbols;
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_HEADER_VALUE,
ERR_HTTP_TRAILER_INVALID,
ERR_INVALID_HTTP_TOKEN,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CHAR,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_WRITE_AFTER_END
} = require('internal/errors').codes;

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

OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
throw new ERR_HTTP_HEADERS_SENT('render');
}

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

this._header = state.header + CRLF;
Expand Down Expand Up @@ -488,12 +498,12 @@ function matchHeader(self, state, field, value) {
function validateHeader(name, value) {
let err;
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name);
err = new ERR_INVALID_HTTP_TOKEN('Header name', name);
} else if (value === undefined) {
err = new errors.TypeError('ERR_HTTP_INVALID_HEADER_VALUE', value, name);
err = new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
} else if (checkInvalidHeaderChar(value)) {
debug('Header "%s" contains invalid characters', name);
err = new errors.TypeError('ERR_INVALID_CHAR', 'header content', name);
err = new ERR_INVALID_CHAR('header content', name);
}
if (err !== undefined) {
Error.captureStackTrace(err, validateHeader);
Expand All @@ -503,7 +513,7 @@ function validateHeader(name, value) {

OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'set');
throw new ERR_HTTP_HEADERS_SENT('set');
}
validateHeader(name, value);

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

OutgoingMessage.prototype.getHeader = function getHeader(name) {
if (typeof name !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
throw new ERR_INVALID_ARG_TYPE('name', 'string');
}

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

OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
if (typeof name !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
throw new ERR_INVALID_ARG_TYPE('name', 'string');
}

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

OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
if (typeof name !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
throw new ERR_INVALID_ARG_TYPE('name', 'string');
}

if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'remove');
throw new ERR_HTTP_HEADERS_SENT('remove');
}

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


OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
throw new errors.Error('ERR_METHOD_NOT_IMPLEMENTED', '_implicitHeader()');
throw new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()');
};

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

function write_(msg, chunk, encoding, callback, fromEnd) {
if (msg.finished) {
const err = new errors.Error('ERR_STREAM_WRITE_AFTER_END');
const err = new ERR_STREAM_WRITE_AFTER_END();
nextTick(msg.socket && msg.socket[async_id_symbol],
writeAfterEndNT.bind(msg),
err,
Expand All @@ -642,8 +652,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
['string', 'Buffer']);
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
}


Expand Down Expand Up @@ -711,12 +720,11 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
value = headers[key];
}
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Trailer name',
field);
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
}
if (checkInvalidHeaderChar(value)) {
debug('Trailer "%s" contains invalid characters', field);
throw new errors.TypeError('ERR_INVALID_CHAR', 'trailer content', field);
throw new ERR_INVALID_CHAR('trailer content', field);
}
this._trailer += field + ': ' + escapeHeaderValue(value) + CRLF;
}
Expand All @@ -742,8 +750,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
var uncork;
if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
['string', 'Buffer']);
throw new ERR_INVALID_ARG_TYPE('first argument', ['string', 'Buffer']);
}
if (!this._header) {
if (typeof chunk === 'string')
Expand Down Expand Up @@ -874,7 +881,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {

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

module.exports = {
Expand Down
13 changes: 8 additions & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const {
getOrSetAsyncId
} = require('internal/async_hooks');
const { IncomingMessage } = require('_http_incoming');
const errors = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_STATUS_CODE,
ERR_INVALID_CHAR
} = require('internal/errors').codes;
const Buffer = require('buffer').Buffer;

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

statusCode |= 0;
if (statusCode < 100 || statusCode > 999) {
throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE',
originalStatusCode);
throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
}


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

if (checkInvalidHeaderChar(this.statusMessage))
throw new errors.Error('ERR_INVALID_CHAR', 'statusMessage');
throw new ERR_INVALID_CHAR('statusMessage');

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

Expand Down
17 changes: 10 additions & 7 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ const debug = util.debuglog('stream');
const BufferList = require('internal/streams/BufferList');
const destroyImpl = require('internal/streams/destroy');
const { getHighWaterMark } = require('internal/streams/state');
const errors = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE,
ERR_STREAM_PUSH_AFTER_EOF,
ERR_STREAM_READ_NOT_IMPLEMENTED,
ERR_STREAM_UNSHIFT_AFTER_END_EVENT
} = require('internal/errors').codes;
const ReadableAsyncIterator = require('internal/streams/async_iterator');
const { emitExperimentalWarning } = require('internal/util');
var StringDecoder;
Expand Down Expand Up @@ -232,12 +237,11 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {

if (addToFront) {
if (state.endEmitted)
stream.emit('error',
new errors.Error('ERR_STREAM_UNSHIFT_AFTER_END_EVENT'));
stream.emit('error', new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
else
addChunk(stream, state, chunk, true);
} else if (state.ended) {
stream.emit('error', new errors.Error('ERR_STREAM_PUSH_AFTER_EOF'));
stream.emit('error', new ERR_STREAM_PUSH_AFTER_EOF());
} else if (state.destroyed) {
return false;
} else {
Expand Down Expand Up @@ -285,8 +289,7 @@ function chunkInvalid(state, chunk) {
typeof chunk !== 'string' &&
chunk !== undefined &&
!state.objectMode) {
er = new errors.TypeError('ERR_INVALID_ARG_TYPE',
'chunk', ['string', 'Buffer', 'Uint8Array']);
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array']);
}
return er;
}
Expand Down Expand Up @@ -565,7 +568,7 @@ function maybeReadMore_(stream, state) {
// for virtual (non-string, non-buffer) streams, "length" is somewhat
// arbitrary, and perhaps not very meaningful.
Readable.prototype._read = function(n) {
this.emit('error', new errors.Error('ERR_STREAM_READ_NOT_IMPLEMENTED'));
this.emit('error', new ERR_STREAM_READ_NOT_IMPLEMENTED());
};

Readable.prototype.pipe = function(dest, pipeOpts) {
Expand Down
15 changes: 10 additions & 5 deletions lib/_stream_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@
'use strict';

module.exports = Transform;
const errors = require('internal/errors');
const {
ERR_METHOD_NOT_IMPLEMENTED,
ERR_MULTIPLE_CALLBACK,
ERR_TRANSFORM_ALREADY_TRANSFORMING,
ERR_TRANSFORM_WITH_LENGTH_0
} = require('internal/errors').codes;
const Duplex = require('_stream_duplex');
const util = require('util');
util.inherits(Transform, Duplex);
Expand All @@ -77,7 +82,7 @@ function afterTransform(er, data) {
var cb = ts.writecb;

if (cb === null) {
return this.emit('error', new errors.Error('ERR_MULTIPLE_CALLBACK'));
return this.emit('error', new ERR_MULTIPLE_CALLBACK());
}

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

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

if (stream._transformState.transforming)
throw new errors.Error('ERR_TRANSFORM_ALREADY_TRANSFORMING');
throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
return stream.push(null);
}
Loading

0 comments on commit 1d2fd8b

Please sign in to comment.