Skip to content

Commit

Permalink
errors: create internal connResetException
Browse files Browse the repository at this point in the history
Replace various instances of errors that use code ECONNRESET with a
single centralized factory function to create the errors.

(While making changes to _tls_wrap.js, this also takes the opportunity
to make trailing commas consistent on multi-line arrays. One had a
trailing comma and one didn't. This adds a traiiling comma to the one
that didn't.)

PR-URL: nodejs#27953
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
Trott committed May 31, 2019
1 parent d05668d commit aa8b820
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
16 changes: 4 additions & 12 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ const { Buffer } = require('buffer');
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const { URL, urlToOptions, searchParamsSymbol } = require('internal/url');
const { outHeadersKey, ondrain } = require('internal/http');
const { connResetException, codes } = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_HTTP_TOKEN,
ERR_INVALID_PROTOCOL,
ERR_UNESCAPED_CHARACTERS
} = require('internal/errors').codes;
} = codes;
const { getTimerDuration } = require('internal/timers');
const {
DTRACE_HTTP_CLIENT_REQUEST,
Expand Down Expand Up @@ -337,15 +338,6 @@ function emitAbortNT() {
this.emit('abort');
}


function createHangUpError() {
// eslint-disable-next-line no-restricted-syntax
const error = new Error('socket hang up');
error.code = 'ECONNRESET';
return error;
}


function socketCloseListener() {
const socket = this;
const req = socket._httpMessage;
Expand Down Expand Up @@ -381,7 +373,7 @@ function socketCloseListener() {
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('error', createHangUpError());
req.emit('error', connResetException('socket hang up'));
}
req.emit('close');
}
Expand Down Expand Up @@ -441,7 +433,7 @@ function socketOnEnd() {
// If we don't have a response then we know that the socket
// ended prematurely and we need to emit an error on the request.
req.socket._hadError = true;
req.emit('error', createHangUpError());
req.emit('error', connResetException('socket hang up'));
}
if (parser) {
parser.finish();
Expand Down
16 changes: 7 additions & 9 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const tls_wrap = internalBinding('tls_wrap');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
const { owner_symbol } = require('internal/async_hooks').symbols;
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
const { connResetException, codes } = require('internal/errors');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK,
Expand All @@ -55,7 +56,7 @@ const {
ERR_TLS_REQUIRED_SERVER_NAME,
ERR_TLS_SESSION_ATTACK,
ERR_TLS_SNI_FROM_SERVER
} = require('internal/errors').codes;
} = codes;
const { getOptionValue } = require('internal/options');
const { validateString } = require('internal/validators');
const traceTls = getOptionValue('--trace-tls');
Expand Down Expand Up @@ -442,7 +443,7 @@ const proxiedMethods = [
'setSimultaneousAccepts', 'setBlocking',

// PipeWrap
'setPendingInstances'
'setPendingInstances',
];

// Proxy HandleWrap, PipeWrap and TCPWrap methods
Expand Down Expand Up @@ -908,9 +909,7 @@ function onSocketClose(err) {
// Emit ECONNRESET
if (!this._controlReleased && !this[kErrorEmitted]) {
this[kErrorEmitted] = true;
// eslint-disable-next-line no-restricted-syntax
const connReset = new Error('socket hang up');
connReset.code = 'ECONNRESET';
const connReset = connResetException('socket hang up');
this._tlsOptions.server.emit('tlsClientError', connReset, this);
}
}
Expand Down Expand Up @@ -1353,10 +1352,9 @@ function onConnectEnd() {
if (!this._hadError) {
const options = this[kConnectOptions];
this._hadError = true;
// eslint-disable-next-line no-restricted-syntax
const error = new Error('Client network socket disconnected before ' +
'secure TLS connection was established');
error.code = 'ECONNRESET';
const error = connResetException('Client network socket disconnected ' +
'before secure TLS connection was ' +
'established');
error.path = options.path;
error.host = options.host;
error.port = options.port;
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,13 @@ function dnsException(code, syscall, hostname) {
return ex;
}

function connResetException(msg) {
// eslint-disable-next-line no-restricted-syntax
const ex = new Error(msg);
ex.code = 'ECONNRESET';
return ex;
}

let maxStack_ErrorName;
let maxStack_ErrorMessage;
/**
Expand Down Expand Up @@ -619,6 +626,7 @@ module.exports = {
getMessage,
hideStackFrames,
isStackOverflowError,
connResetException,
uvException,
uvExceptionWithHostPort,
SystemError,
Expand Down

0 comments on commit aa8b820

Please sign in to comment.