Skip to content

Commit f30b911

Browse files
RaisinTentargos
authored andcommitted
http2: add diagnostics channel 'http2.client.stream.error'
Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #58306 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tierney Cyren <hello@bnb.im> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
1 parent 2d5a1ef commit f30b911

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

doc/api/diagnostics_channel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,13 @@ Emitted when a stream is created on the client.
12171217

12181218
Emitted when a stream is started on the client.
12191219

1220+
`http2.client.stream.error`
1221+
1222+
* `stream` {ClientHttp2Stream}
1223+
* `error` {Error}
1224+
1225+
Emitted when an error occurs during the processing of a stream on the client.
1226+
12201227
#### Modules
12211228

12221229
> Stability: 1 - Experimental

lib/internal/http2/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const { _connectionListener: httpConnectionListener } = http;
187187
const dc = require('diagnostics_channel');
188188
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
189189
const onClientStreamStartChannel = dc.channel('http2.client.stream.start');
190+
const onClientStreamErrorChannel = dc.channel('http2.client.stream.error');
190191

191192
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
192193
debug = fn;
@@ -2424,6 +2425,14 @@ class Http2Stream extends Duplex {
24242425
setImmediate(() => {
24252426
session[kMaybeDestroy]();
24262427
});
2428+
if (err &&
2429+
session[kType] === NGHTTP2_SESSION_CLIENT &&
2430+
onClientStreamErrorChannel.hasSubscribers) {
2431+
onClientStreamErrorChannel.publish({
2432+
stream: this,
2433+
error: err,
2434+
});
2435+
}
24272436
callback(err);
24282437
}
24292438
// The Http2Stream can be destroyed if it has closed and if the readable
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// This test ensures that the built-in HTTP/2 diagnostics channels are reporting
8+
// the diagnostics messages for the 'http2.client.stream.error' channel when
9+
// an error occurs during the processing of a ClientHttp2Stream.
10+
11+
const assert = require('assert');
12+
const dc = require('diagnostics_channel');
13+
const http2 = require('http2');
14+
const { Duplex } = require('stream');
15+
16+
dc.subscribe('http2.client.stream.error', common.mustCall(({ stream, error }) => {
17+
// Since ClientHttp2Stream is not exported from any module, this just checks
18+
// if the stream is an instance of Duplex and the constructor name is
19+
// 'ClientHttp2Stream'.
20+
assert.ok(stream instanceof Duplex);
21+
assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream');
22+
assert.strictEqual(stream.closed, true);
23+
assert.strictEqual(stream.destroyed, true);
24+
25+
assert.ok(error);
26+
assert.strictEqual(error.code, 'ABORT_ERR');
27+
assert.strictEqual(error.name, 'AbortError');
28+
}));
29+
30+
const server = http2.createServer();
31+
server.listen(0, common.mustCall(() => {
32+
const port = server.address().port;
33+
const client = http2.connect(`http://localhost:${port}`);
34+
35+
const ac = new AbortController();
36+
const stream = client.request({}, { signal: ac.signal });
37+
ac.abort();
38+
39+
stream.on('error', common.mustCall((err) => {
40+
assert.strictEqual(err.code, 'ABORT_ERR');
41+
assert.strictEqual(err.name, 'AbortError');
42+
43+
client.close();
44+
server.close();
45+
}));
46+
}));

0 commit comments

Comments
 (0)