Skip to content

Commit aa8b820

Browse files
committed
errors: create internal connResetException
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: #27953 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent d05668d commit aa8b820

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

lib/_http_client.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ const { Buffer } = require('buffer');
4040
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
4141
const { URL, urlToOptions, searchParamsSymbol } = require('internal/url');
4242
const { outHeadersKey, ondrain } = require('internal/http');
43+
const { connResetException, codes } = require('internal/errors');
4344
const {
4445
ERR_HTTP_HEADERS_SENT,
4546
ERR_INVALID_ARG_TYPE,
4647
ERR_INVALID_HTTP_TOKEN,
4748
ERR_INVALID_PROTOCOL,
4849
ERR_UNESCAPED_CHARACTERS
49-
} = require('internal/errors').codes;
50+
} = codes;
5051
const { getTimerDuration } = require('internal/timers');
5152
const {
5253
DTRACE_HTTP_CLIENT_REQUEST,
@@ -337,15 +338,6 @@ function emitAbortNT() {
337338
this.emit('abort');
338339
}
339340

340-
341-
function createHangUpError() {
342-
// eslint-disable-next-line no-restricted-syntax
343-
const error = new Error('socket hang up');
344-
error.code = 'ECONNRESET';
345-
return error;
346-
}
347-
348-
349341
function socketCloseListener() {
350342
const socket = this;
351343
const req = socket._httpMessage;
@@ -381,7 +373,7 @@ function socketCloseListener() {
381373
// receive a response. The error needs to
382374
// fire on the request.
383375
req.socket._hadError = true;
384-
req.emit('error', createHangUpError());
376+
req.emit('error', connResetException('socket hang up'));
385377
}
386378
req.emit('close');
387379
}
@@ -441,7 +433,7 @@ function socketOnEnd() {
441433
// If we don't have a response then we know that the socket
442434
// ended prematurely and we need to emit an error on the request.
443435
req.socket._hadError = true;
444-
req.emit('error', createHangUpError());
436+
req.emit('error', connResetException('socket hang up'));
445437
}
446438
if (parser) {
447439
parser.finish();

lib/_tls_wrap.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const tls_wrap = internalBinding('tls_wrap');
4444
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
4545
const { owner_symbol } = require('internal/async_hooks').symbols;
4646
const { SecureContext: NativeSecureContext } = internalBinding('crypto');
47+
const { connResetException, codes } = require('internal/errors');
4748
const {
4849
ERR_INVALID_ARG_TYPE,
4950
ERR_INVALID_CALLBACK,
@@ -55,7 +56,7 @@ const {
5556
ERR_TLS_REQUIRED_SERVER_NAME,
5657
ERR_TLS_SESSION_ATTACK,
5758
ERR_TLS_SNI_FROM_SERVER
58-
} = require('internal/errors').codes;
59+
} = codes;
5960
const { getOptionValue } = require('internal/options');
6061
const { validateString } = require('internal/validators');
6162
const traceTls = getOptionValue('--trace-tls');
@@ -442,7 +443,7 @@ const proxiedMethods = [
442443
'setSimultaneousAccepts', 'setBlocking',
443444

444445
// PipeWrap
445-
'setPendingInstances'
446+
'setPendingInstances',
446447
];
447448

448449
// Proxy HandleWrap, PipeWrap and TCPWrap methods
@@ -908,9 +909,7 @@ function onSocketClose(err) {
908909
// Emit ECONNRESET
909910
if (!this._controlReleased && !this[kErrorEmitted]) {
910911
this[kErrorEmitted] = true;
911-
// eslint-disable-next-line no-restricted-syntax
912-
const connReset = new Error('socket hang up');
913-
connReset.code = 'ECONNRESET';
912+
const connReset = connResetException('socket hang up');
914913
this._tlsOptions.server.emit('tlsClientError', connReset, this);
915914
}
916915
}
@@ -1353,10 +1352,9 @@ function onConnectEnd() {
13531352
if (!this._hadError) {
13541353
const options = this[kConnectOptions];
13551354
this._hadError = true;
1356-
// eslint-disable-next-line no-restricted-syntax
1357-
const error = new Error('Client network socket disconnected before ' +
1358-
'secure TLS connection was established');
1359-
error.code = 'ECONNRESET';
1355+
const error = connResetException('Client network socket disconnected ' +
1356+
'before secure TLS connection was ' +
1357+
'established');
13601358
error.path = options.path;
13611359
error.host = options.host;
13621360
error.port = options.port;

lib/internal/errors.js

+8
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ function dnsException(code, syscall, hostname) {
554554
return ex;
555555
}
556556

557+
function connResetException(msg) {
558+
// eslint-disable-next-line no-restricted-syntax
559+
const ex = new Error(msg);
560+
ex.code = 'ECONNRESET';
561+
return ex;
562+
}
563+
557564
let maxStack_ErrorName;
558565
let maxStack_ErrorMessage;
559566
/**
@@ -619,6 +626,7 @@ module.exports = {
619626
getMessage,
620627
hideStackFrames,
621628
isStackOverflowError,
629+
connResetException,
622630
uvException,
623631
uvExceptionWithHostPort,
624632
SystemError,

0 commit comments

Comments
 (0)