diff --git a/doc/api/errors.md b/doc/api/errors.md index fa56c7a5295ab6..2f7f60983eed7a 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -837,7 +837,7 @@ requests and responses. ### ERR_HTTP2_INVALID_HEADER_VALUE -Used to indicate that an invalid HTTP/2 header value has been specified. +Used to indicate that an invalid HTTP2 header value has been specified. ### ERR_HTTP2_INVALID_INFO_STATUS diff --git a/lib/internal/errors.js b/lib/internal/errors.js index d13a8e85de0917..da00ba25915d0e 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -280,7 +280,7 @@ E('ERR_HTTP2_INFO_STATUS_NOT_ALLOWED', 'Informational status codes cannot be used'); E('ERR_HTTP2_INVALID_CONNECTION_HEADERS', 'HTTP/1 Connection specific headers are forbidden: "%s"'); -E('ERR_HTTP2_INVALID_HEADER_VALUE', 'Value must not be undefined or null'); +E('ERR_HTTP2_INVALID_HEADER_VALUE', 'Invalid value "%s" for header "%s"'); E('ERR_HTTP2_INVALID_INFO_STATUS', (code) => `Invalid informational status code: ${code}`); E('ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH', diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index c96fc931969f39..89a22270086418 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -40,12 +40,18 @@ let statusMessageWarned = false; // close as possible to the current require('http') API function assertValidHeader(name, value) { - if (name === '' || typeof name !== 'string') - throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); - if (isPseudoHeader(name)) - throw new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'); - if (value === undefined || value === null) - throw new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE'); + let err; + if (name === '' || typeof name !== 'string') { + err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name); + } else if (isPseudoHeader(name)) { + err = new errors.Error('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'); + } else if (value === undefined || value === null) { + err = new errors.TypeError('ERR_HTTP2_INVALID_HEADER_VALUE', value, name); + } + if (err !== undefined) { + Error.captureStackTrace(err, assertValidHeader); + throw err; + } } function isPseudoHeader(name) { diff --git a/test/parallel/test-http2-compat-serverresponse-headers.js b/test/parallel/test-http2-compat-serverresponse-headers.js index aae1d96c552ebe..ec1071bc34ffa1 100644 --- a/test/parallel/test-http2-compat-serverresponse-headers.js +++ b/test/parallel/test-http2-compat-serverresponse-headers.js @@ -85,14 +85,14 @@ server.listen(0, common.mustCall(function() { }, common.expectsError({ code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "null" for header "foo-bar"' })); assert.throws(function() { response.setHeader(real, undefined); }, common.expectsError({ code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "undefined" for header "foo-bar"' })); common.expectsError( () => response.setHeader(), // header name undefined diff --git a/test/parallel/test-http2-compat-serverresponse-trailers.js b/test/parallel/test-http2-compat-serverresponse-trailers.js index 4d93e14dbec4b4..7332f9e8d0b63d 100644 --- a/test/parallel/test-http2-compat-serverresponse-trailers.js +++ b/test/parallel/test-http2-compat-serverresponse-trailers.js @@ -28,7 +28,7 @@ server.listen(0, common.mustCall(() => { { code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "undefined" for header "test"' } ); common.expectsError( @@ -36,7 +36,7 @@ server.listen(0, common.mustCall(() => { { code: 'ERR_HTTP2_INVALID_HEADER_VALUE', type: TypeError, - message: 'Value must not be undefined or null' + message: 'Invalid value "null" for header "test"' } ); common.expectsError(