Skip to content

Commit 8e64c02

Browse files
cola119marco-ippolito
authored andcommitted
http: add diagnostics channel http.client.request.error
PR-URL: #54054 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]>
1 parent c0262c1 commit 8e64c02

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

doc/api/diagnostics_channel.md

+7
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,13 @@ independently.
11191119

11201120
Emitted when client starts a request.
11211121

1122+
`http.client.request.error`
1123+
1124+
* `request` {http.ClientRequest}
1125+
* `error` {Error}
1126+
1127+
Emitted when an error occurs during a client request.
1128+
11221129
`http.client.response.finish`
11231130

11241131
* `request` {http.ClientRequest}

lib/_http_client.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,19 @@ const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9595

9696
const dc = require('diagnostics_channel');
9797
const onClientRequestStartChannel = dc.channel('http.client.request.start');
98+
const onClientRequestErrorChannel = dc.channel('http.client.request.error');
9899
const onClientResponseFinishChannel = dc.channel('http.client.response.finish');
99100

101+
function emitErrorEvent(request, error) {
102+
if (onClientRequestErrorChannel.hasSubscribers) {
103+
onClientRequestErrorChannel.publish({
104+
request,
105+
error,
106+
});
107+
}
108+
request.emit('error', error);
109+
}
110+
100111
const { addAbortSignal, finished } = require('stream');
101112

102113
let debug = require('internal/util/debuglog').debuglog('http', (fn) => {
@@ -348,7 +359,7 @@ function ClientRequest(input, options, cb) {
348359
if (typeof opts.createConnection === 'function') {
349360
const oncreate = once((err, socket) => {
350361
if (err) {
351-
process.nextTick(() => this.emit('error', err));
362+
process.nextTick(() => emitErrorEvent(this, err));
352363
} else {
353364
this.onSocket(socket);
354365
}
@@ -470,7 +481,7 @@ function socketCloseListener() {
470481
// receive a response. The error needs to
471482
// fire on the request.
472483
req.socket._hadError = true;
473-
req.emit('error', new ConnResetException('socket hang up'));
484+
emitErrorEvent(req, new ConnResetException('socket hang up'));
474485
}
475486
req._closed = true;
476487
req.emit('close');
@@ -497,7 +508,7 @@ function socketErrorListener(err) {
497508
// For Safety. Some additional errors might fire later on
498509
// and we need to make sure we don't double-fire the error event.
499510
req.socket._hadError = true;
500-
req.emit('error', err);
511+
emitErrorEvent(req, err);
501512
}
502513

503514
const parser = socket.parser;
@@ -521,7 +532,7 @@ function socketOnEnd() {
521532
// If we don't have a response then we know that the socket
522533
// ended prematurely and we need to emit an error on the request.
523534
req.socket._hadError = true;
524-
req.emit('error', new ConnResetException('socket hang up'));
535+
emitErrorEvent(req, new ConnResetException('socket hang up'));
525536
}
526537
if (parser) {
527538
parser.finish();
@@ -546,7 +557,7 @@ function socketOnData(d) {
546557
socket.removeListener('end', socketOnEnd);
547558
socket.destroy();
548559
req.socket._hadError = true;
549-
req.emit('error', ret);
560+
emitErrorEvent(req, ret);
550561
} else if (parser.incoming && parser.incoming.upgrade) {
551562
// Upgrade (if status code 101) or CONNECT
552563
const bytesParsed = ret;
@@ -877,7 +888,7 @@ function onSocketNT(req, socket, err) {
877888
err = new ConnResetException('socket hang up');
878889
}
879890
if (err) {
880-
req.emit('error', err);
891+
emitErrorEvent(req, err);
881892
}
882893
req._closed = true;
883894
req.emit('close');

test/parallel/test-diagnostics-channel-http.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const common = require('../common');
3+
const { addresses } = require('../common/internet');
34
const assert = require('assert');
45
const http = require('http');
56
const net = require('net');
@@ -9,9 +10,15 @@ const isHTTPServer = (server) => server instanceof http.Server;
910
const isIncomingMessage = (object) => object instanceof http.IncomingMessage;
1011
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage;
1112
const isNetSocket = (socket) => socket instanceof net.Socket;
13+
const isError = (error) => error instanceof Error;
1214

1315
dc.subscribe('http.client.request.start', common.mustCall(({ request }) => {
1416
assert.strictEqual(isOutgoingMessage(request), true);
17+
}, 2));
18+
19+
dc.subscribe('http.client.request.error', common.mustCall(({ request, error }) => {
20+
assert.strictEqual(isOutgoingMessage(request), true);
21+
assert.strictEqual(isError(error), true);
1522
}));
1623

1724
dc.subscribe('http.client.response.finish', common.mustCall(({
@@ -50,8 +57,14 @@ const server = http.createServer(common.mustCall((req, res) => {
5057
res.end('done');
5158
}));
5259

53-
server.listen(() => {
60+
server.listen(async () => {
5461
const { port } = server.address();
62+
const invalidRequest = http.get({
63+
host: addresses.INVALID_HOST,
64+
});
65+
await new Promise((resolve) => {
66+
invalidRequest.on('error', resolve);
67+
});
5568
http.get(`http://localhost:${port}`, (res) => {
5669
res.resume();
5770
res.on('end', () => {

0 commit comments

Comments
 (0)