Skip to content

Commit 441175c

Browse files
addaleaxtargos
authored andcommitted
http2: refer to stream errors by name
Display the constant name instead of a stream error code in the error message, because the numerical codes give absolutely no clue about what happened when an error is emitted. Backport-PR-URL: #19579 PR-URL: #18966 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 4caf536 commit 441175c

20 files changed

+61
-46
lines changed

lib/internal/http2/core.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const {
6262
} = require('timers');
6363

6464
const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
65-
const { constants } = binding;
65+
const { constants, nameForErrorCode } = binding;
6666

6767
const NETServer = net.Server;
6868
const TLSServer = tls.Server;
@@ -1841,7 +1841,8 @@ class Http2Stream extends Duplex {
18411841
// abort and is already covered by aborted event, also allows more
18421842
// seamless compatibility with http1
18431843
if (err == null && code !== NGHTTP2_NO_ERROR && code !== NGHTTP2_CANCEL)
1844-
err = new errors.Error('ERR_HTTP2_STREAM_ERROR', code);
1844+
err = new errors.Error('ERR_HTTP2_STREAM_ERROR',
1845+
nameForErrorCode[code] || code);
18451846

18461847
this[kSession] = undefined;
18471848
this[kHandle] = undefined;

src/node_http2.cc

+36-23
Original file line numberDiff line numberDiff line change
@@ -2857,29 +2857,39 @@ void Initialize(Local<Object> target,
28572857
session->GetFunction()).FromJust();
28582858

28592859
Local<Object> constants = Object::New(isolate);
2860-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SESSION_SERVER);
2861-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SESSION_CLIENT);
2862-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_IDLE);
2863-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_OPEN);
2864-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_RESERVED_LOCAL);
2865-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_RESERVED_REMOTE);
2866-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL);
2867-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE);
2868-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_CLOSED);
2869-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_NO_ERROR);
2870-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_PROTOCOL_ERROR);
2871-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_INTERNAL_ERROR);
2872-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLOW_CONTROL_ERROR);
2873-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_TIMEOUT);
2874-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_CLOSED);
2875-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FRAME_SIZE_ERROR);
2876-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_REFUSED_STREAM);
2877-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_CANCEL);
2878-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_COMPRESSION_ERROR);
2879-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_CONNECT_ERROR);
2880-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_ENHANCE_YOUR_CALM);
2881-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_INADEQUATE_SECURITY);
2882-
NODE_DEFINE_CONSTANT(constants, NGHTTP2_HTTP_1_1_REQUIRED);
2860+
Local<Array> name_for_error_code = Array::New(isolate);
2861+
2862+
#define NODE_NGHTTP2_ERROR_CODES(V) \
2863+
V(NGHTTP2_SESSION_SERVER); \
2864+
V(NGHTTP2_SESSION_CLIENT); \
2865+
V(NGHTTP2_STREAM_STATE_IDLE); \
2866+
V(NGHTTP2_STREAM_STATE_OPEN); \
2867+
V(NGHTTP2_STREAM_STATE_RESERVED_LOCAL); \
2868+
V(NGHTTP2_STREAM_STATE_RESERVED_REMOTE); \
2869+
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL); \
2870+
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE); \
2871+
V(NGHTTP2_STREAM_STATE_CLOSED); \
2872+
V(NGHTTP2_NO_ERROR); \
2873+
V(NGHTTP2_PROTOCOL_ERROR); \
2874+
V(NGHTTP2_INTERNAL_ERROR); \
2875+
V(NGHTTP2_FLOW_CONTROL_ERROR); \
2876+
V(NGHTTP2_SETTINGS_TIMEOUT); \
2877+
V(NGHTTP2_STREAM_CLOSED); \
2878+
V(NGHTTP2_FRAME_SIZE_ERROR); \
2879+
V(NGHTTP2_REFUSED_STREAM); \
2880+
V(NGHTTP2_CANCEL); \
2881+
V(NGHTTP2_COMPRESSION_ERROR); \
2882+
V(NGHTTP2_CONNECT_ERROR); \
2883+
V(NGHTTP2_ENHANCE_YOUR_CALM); \
2884+
V(NGHTTP2_INADEQUATE_SECURITY); \
2885+
V(NGHTTP2_HTTP_1_1_REQUIRED); \
2886+
2887+
#define V(name) \
2888+
NODE_DEFINE_CONSTANT(constants, name); \
2889+
name_for_error_code->Set(static_cast<int>(name), \
2890+
FIXED_ONE_BYTE_STRING(isolate, #name));
2891+
NODE_NGHTTP2_ERROR_CODES(V)
2892+
#undef V
28832893

28842894
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_REQUEST);
28852895
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_RESPONSE);
@@ -2944,6 +2954,9 @@ HTTP_STATUS_CODES(V)
29442954
target->Set(context,
29452955
FIXED_ONE_BYTE_STRING(isolate, "constants"),
29462956
constants).FromJust();
2957+
target->Set(context,
2958+
FIXED_ONE_BYTE_STRING(isolate, "nameForErrorCode"),
2959+
name_for_error_code).FromJust();
29472960
}
29482961
} // namespace http2
29492962
} // namespace node

test/parallel/test-http2-client-rststream-before-connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ server.listen(0, common.mustCall(() => {
4747
req.on('error', common.expectsError({
4848
code: 'ERR_HTTP2_STREAM_ERROR',
4949
type: Error,
50-
message: `Stream closed with error code ${closeCode}`
50+
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
5151
}));
5252

5353
req.on('response', common.mustCall());

test/parallel/test-http2-client-stream-destroy-before-connect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ server.on('stream', (stream) => {
1818
// system specific timings.
1919
stream.on('error', (err) => {
2020
assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
21-
assert.strictEqual(err.message, 'Stream closed with error code 2');
21+
assert.strictEqual(err.message,
22+
'Stream closed with error code NGHTTP2_INTERNAL_ERROR');
2223
});
2324
stream.respond();
2425
stream.end();

test/parallel/test-http2-client-unescaped-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ server.listen(0, common.mustCall(() => {
2727
req.on('error', common.expectsError({
2828
code: 'ERR_HTTP2_STREAM_ERROR',
2929
type: Error,
30-
message: 'Stream closed with error code 1'
30+
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
3131
}));
3232
req.on('close', common.mustCall(() => countdown.dec()));
3333
}

test/parallel/test-http2-compat-serverresponse-destroy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ server.listen(0, common.mustCall(() => {
5858
req.on('error', common.expectsError({
5959
code: 'ERR_HTTP2_STREAM_ERROR',
6060
type: Error,
61-
message: 'Stream closed with error code 2'
61+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
6262
}));
6363
req.on('close', common.mustCall(() => countdown.dec()));
6464

@@ -73,7 +73,7 @@ server.listen(0, common.mustCall(() => {
7373
req.on('error', common.expectsError({
7474
code: 'ERR_HTTP2_STREAM_ERROR',
7575
type: Error,
76-
message: 'Stream closed with error code 2'
76+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
7777
}));
7878
req.on('close', common.mustCall(() => countdown.dec()));
7979

test/parallel/test-http2-info-headers-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function runTest(test) {
7272
req.on('error', common.expectsError({
7373
code: 'ERR_HTTP2_STREAM_ERROR',
7474
type: Error,
75-
message: 'Stream closed with error code 2'
75+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
7676
}));
7777

7878
req.on('close', common.mustCall(() => {

test/parallel/test-http2-max-concurrent-streams.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ server.listen(0, common.mustCall(() => {
5050
req.on('error', common.expectsError({
5151
code: 'ERR_HTTP2_STREAM_ERROR',
5252
type: Error,
53-
message: 'Stream closed with error code 7'
53+
message: 'Stream closed with error code NGHTTP2_REFUSED_STREAM'
5454
}));
5555
}
5656
}));

test/parallel/test-http2-misbehaving-flow-control-paused.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ server.on('stream', (stream) => {
6363
stream.on('error', common.expectsError({
6464
code: 'ERR_HTTP2_STREAM_ERROR',
6565
type: Error,
66-
message: 'Stream closed with error code 3'
66+
message: 'Stream closed with error code NGHTTP2_FLOW_CONTROL_ERROR'
6767
}));
6868
stream.on('close', common.mustCall(() => {
6969
server.close();

test/parallel/test-http2-misbehaving-flow-control.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ server.on('stream', (stream) => {
6969
stream.on('error', common.expectsError({
7070
code: 'ERR_HTTP2_STREAM_ERROR',
7171
type: Error,
72-
message: 'Stream closed with error code 3'
72+
message: 'Stream closed with error code NGHTTP2_FLOW_CONTROL_ERROR'
7373
}));
7474
stream.on('close', common.mustCall(() => {
7575
server.close(common.mustCall());

test/parallel/test-http2-misused-pseudoheaders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ server.listen(0, common.mustCall(() => {
4141
req.on('error', common.expectsError({
4242
code: 'ERR_HTTP2_STREAM_ERROR',
4343
type: Error,
44-
message: 'Stream closed with error code 2'
44+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
4545
}));
4646

4747
req.on('response', common.mustCall());

test/parallel/test-http2-multi-content-length.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ server.listen(0, common.mustCall(() => {
5858
req.on('error', common.expectsError({
5959
code: 'ERR_HTTP2_STREAM_ERROR',
6060
type: Error,
61-
message: 'Stream closed with error code 1'
61+
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
6262
}));
6363
}
6464
}));

test/parallel/test-http2-options-max-headers-block-length.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ server.listen(0, common.mustCall(() => {
3838
req.on('error', common.expectsError({
3939
code: 'ERR_HTTP2_STREAM_ERROR',
4040
type: Error,
41-
message: 'Stream closed with error code 7'
41+
message: 'Stream closed with error code NGHTTP2_REFUSED_STREAM'
4242
}));
4343
}));

test/parallel/test-http2-respond-file-fd-invalid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
const errorCheck = common.expectsError({
1616
code: 'ERR_HTTP2_STREAM_ERROR',
1717
type: Error,
18-
message: `Stream closed with error code ${NGHTTP2_INTERNAL_ERROR}`
18+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
1919
}, 2);
2020

2121
const server = http2.createServer();

test/parallel/test-http2-respond-nghttperrors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function runTest(test) {
8080
req.on('error', common.expectsError({
8181
code: 'ERR_HTTP2_STREAM_ERROR',
8282
type: Error,
83-
message: 'Stream closed with error code 2'
83+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
8484
}));
8585

8686
currentError = test;

test/parallel/test-http2-respond-with-fd-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function runTest(test) {
8888
req.on('error', common.expectsError({
8989
code: 'ERR_HTTP2_STREAM_ERROR',
9090
type: Error,
91-
message: 'Stream closed with error code 2'
91+
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
9292
}));
9393

9494
currentError = test;

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

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

2727
const server = http2.createServer();

test/parallel/test-http2-too-large-headers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ server.listen(0, common.mustCall(() => {
2020
req.on('error', common.expectsError({
2121
code: 'ERR_HTTP2_STREAM_ERROR',
2222
type: Error,
23-
message: 'Stream closed with error code 11'
23+
message: 'Stream closed with error code NGHTTP2_ENHANCE_YOUR_CALM'
2424
}));
2525
req.on('close', common.mustCall((code) => {
2626
assert.strictEqual(code, NGHTTP2_ENHANCE_YOUR_CALM);

test/parallel/test-http2-too-many-headers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ server.listen(0, common.mustCall(() => {
2323
req.on('error', common.expectsError({
2424
code: 'ERR_HTTP2_STREAM_ERROR',
2525
type: Error,
26-
message: 'Stream closed with error code 11'
26+
message: 'Stream closed with error code NGHTTP2_ENHANCE_YOUR_CALM'
2727
}));
2828
req.on('close', common.mustCall((code) => {
2929
assert.strictEqual(code, NGHTTP2_ENHANCE_YOUR_CALM);

test/sequential/test-http2-max-session-memory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ server.listen(0, common.mustCall(() => {
3030
req.on('error', common.expectsError({
3131
code: 'ERR_HTTP2_STREAM_ERROR',
3232
type: Error,
33-
message: 'Stream closed with error code 11'
33+
message: 'Stream closed with error code NGHTTP2_ENHANCE_YOUR_CALM'
3434
}));
3535
req.on('close', common.mustCall(() => {
3636
server.close();

0 commit comments

Comments
 (0)