Skip to content

Commit 3914e97

Browse files
mcollinaMylesBorins
authored andcommitted
http2: fixes error handling
There should be no default error handling when using Http2Stream. All errors will end up in `'streamError'` on the server anyway, but they are emitted on `'stream'` as well, otherwise some error conditions are impossible to debug. See: #14991 Backport-PR-URL: #19478 PR-URL: #19232 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 55f7bbb commit 3914e97

6 files changed

+35
-12
lines changed

lib/internal/http2/core.js

-7
Original file line numberDiff line numberDiff line change
@@ -2041,19 +2041,12 @@ function afterOpen(session, options, headers, streamOptions, err, fd) {
20412041
headers, streamOptions));
20422042
}
20432043

2044-
function streamOnError(err) {
2045-
// we swallow the error for parity with HTTP1
2046-
// all the errors that ends here are not critical for the project
2047-
}
2048-
2049-
20502044
class ServerHttp2Stream extends Http2Stream {
20512045
constructor(session, handle, id, options, headers) {
20522046
super(session, options);
20532047
this[kInit](id, handle);
20542048
this[kProtocol] = headers[HTTP2_HEADER_SCHEME];
20552049
this[kAuthority] = headers[HTTP2_HEADER_AUTHORITY];
2056-
this.on('error', streamOnError);
20572050
}
20582051

20592052
// true if the remote peer accepts push streams

test/parallel/test-http2-misbehaving-multiplex.js

+7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const server = h2.createServer();
1414
server.on('stream', common.mustCall((stream) => {
1515
stream.respond();
1616
stream.end('ok');
17+
18+
// the error will be emitted asynchronously
19+
stream.on('error', common.expectsError({
20+
code: 'ERR_HTTP2_ERROR',
21+
message: 'Stream was already closed or invalid'
22+
}));
1723
}, 2));
24+
1825
server.on('session', common.mustCall((session) => {
1926
session.on('error', common.expectsError({
2027
code: 'ERR_HTTP2_ERROR',

test/parallel/test-http2-server-errors.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,16 @@ const h2 = require('http2');
5454

5555
const server = h2.createServer();
5656

57+
process.on('uncaughtException', common.mustCall(function(err) {
58+
assert.strictEqual(err.message, 'kaboom no handler');
59+
}));
60+
5761
server.on('stream', common.mustCall(function(stream) {
58-
// there is no 'error' handler, and this will not crash
62+
// there is no 'error' handler, and this will crash
5963
stream.write('hello');
6064
stream.resume();
6165

62-
expected = new Error('kaboom');
66+
expected = new Error('kaboom no handler');
6367
stream.destroy(expected);
6468
server.close();
6569
}));

test/parallel/test-http2-server-push-stream-head.js

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ server.on('stream', common.mustCall((stream, headers) => {
2121
}, common.mustCall((err, push, headers) => {
2222
assert.strictEqual(push._writableState.ended, true);
2323
push.respond();
24+
// cannot write to push() anymore
25+
push.on('error', common.expectsError({
26+
type: Error,
27+
code: 'ERR_STREAM_WRITE_AFTER_END',
28+
message: 'write after end'
29+
}));
2430
assert(!push.write('test'));
2531
stream.end('test');
2632
}));

test/parallel/test-http2-server-rst-stream.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@ const {
1818
const tests = [
1919
[NGHTTP2_NO_ERROR, false],
2020
[NGHTTP2_NO_ERROR, false],
21-
[NGHTTP2_PROTOCOL_ERROR, true],
21+
[NGHTTP2_PROTOCOL_ERROR, true, 1],
2222
[NGHTTP2_CANCEL, false],
23-
[NGHTTP2_REFUSED_STREAM, true],
24-
[NGHTTP2_INTERNAL_ERROR, true]
23+
[NGHTTP2_REFUSED_STREAM, true, 7],
24+
[NGHTTP2_INTERNAL_ERROR, true, 2]
2525
];
2626

2727
const server = http2.createServer();
2828
server.on('stream', (stream, headers) => {
29+
const test = tests.find((t) => t[0] === Number(headers.rstcode));
30+
if (test[1]) {
31+
stream.on('error', common.expectsError({
32+
type: Error,
33+
code: 'ERR_HTTP2_STREAM_ERROR',
34+
message: `Stream closed with error code ${test[2]}`
35+
}));
36+
}
2937
stream.close(headers.rstcode | 0);
3038
});
3139

test/parallel/test-http2-server-stream-session-destroy.js

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ server.on('stream', common.mustCall((stream) => {
3434
type: Error
3535
}
3636
);
37+
stream.on('error', common.expectsError({
38+
type: Error,
39+
code: 'ERR_STREAM_WRITE_AFTER_END',
40+
message: 'write after end'
41+
}));
3742
assert.strictEqual(stream.write('data'), false);
3843
}));
3944

0 commit comments

Comments
 (0)