Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8e64c02

Browse files
cola119marco-ippolito
authored andcommitted
http: add diagnostics channelhttp.client.request.error
PR-URL:#54054Reviewed-By: Paolo Insogna <paolo@cowtech.it>Reviewed-By: Matteo Collina <matteo.collina@gmail.com>Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parentc0262c1 commit8e64c02

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

‎doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions
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

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

9696
constdc=require('diagnostics_channel');
9797
constonClientRequestStartChannel=dc.channel('http.client.request.start');
98+
constonClientRequestErrorChannel=dc.channel('http.client.request.error');
9899
constonClientResponseFinishChannel=dc.channel('http.client.response.finish');
99100

101+
functionemitErrorEvent(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
letdebug=require('internal/util/debuglog').debuglog('http',(fn)=>{
@@ -348,7 +359,7 @@ function ClientRequest(input, options, cb) {
348359
if(typeofopts.createConnection==='function'){
349360
constoncreate=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',newConnResetException('socket hang up'));
484+
emitErrorEvent(req,newConnResetException('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
constparser=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',newConnResetException('socket hang up'));
535+
emitErrorEvent(req,newConnResetException('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
}elseif(parser.incoming&&parser.incoming.upgrade){
551562
// Upgrade (if status code 101) or CONNECT
552563
constbytesParsed=ret;
@@ -877,7 +888,7 @@ function onSocketNT(req, socket, err) {
877888
err=newConnResetException('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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
constcommon=require('../common');
3+
const{ addresses}=require('../common/internet');
34
constassert=require('assert');
45
consthttp=require('http');
56
constnet=require('net');
@@ -9,9 +10,15 @@ const isHTTPServer = (server) => server instanceof http.Server;
910
constisIncomingMessage=(object)=>objectinstanceofhttp.IncomingMessage;
1011
constisOutgoingMessage=(object)=>objectinstanceofhttp.OutgoingMessage;
1112
constisNetSocket=(socket)=>socketinstanceofnet.Socket;
13+
constisError=(error)=>errorinstanceofError;
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+
constinvalidRequest=http.get({
63+
host:addresses.INVALID_HOST,
64+
});
65+
awaitnewPromise((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)

[8]ページ先頭

©2009-2025 Movatter.jp