Skip to content

Commit b15a8bd

Browse files
committed
http2: add diagnostics channel 'http2.client.stream.error'
Signed-off-by: Darshan Sen <[email protected]>
1 parent 6e222ed commit b15a8bd

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
@@ -1210,6 +1210,13 @@ Emitted when server sends a response.
12101210

12111211
Emitted when a stream is created on the client.
12121212

1213+
`http2.client.stream.error`
1214+
1215+
* `stream` {ClientHttp2Stream}
1216+
* `error` {Error}
1217+
1218+
Emitted when an error occurs during the processing of a stream on the client.
1219+
12131220
#### Modules
12141221

12151222
`module.require.start`

lib/internal/http2/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ const { _connectionListener: httpConnectionListener } = http;
186186

187187
const dc = require('diagnostics_channel');
188188
const onClientStreamCreatedChannel = dc.channel('http2.client.stream.created');
189+
const onClientStreamErrorChannel = dc.channel('http2.client.stream.error');
189190

190191
let debug = require('internal/util/debuglog').debuglog('http2', (fn) => {
191192
debug = fn;
@@ -2411,6 +2412,14 @@ class Http2Stream extends Duplex {
24112412
setImmediate(() => {
24122413
session[kMaybeDestroy]();
24132414
});
2415+
if (err &&
2416+
session[kType] === NGHTTP2_SESSION_CLIENT &&
2417+
onClientStreamErrorChannel.hasSubscribers) {
2418+
onClientStreamErrorChannel.publish({
2419+
stream: this,
2420+
error: err,
2421+
});
2422+
}
24142423
callback(err);
24152424
}
24162425
// 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)