http: improve parser error messages

Include the library-provided reason in the Error’s `message`.

Fixes: https://github.com/nodejs/node/issues/28468

PR-URL: https://github.com/nodejs/node/pull/28487
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Anna Henningsen
2019-07-01 00:34:55 +02:00
committed by Rich Trott
parent fd23c12263
commit ba565a3734
8 changed files with 49 additions and 6 deletions

View File

@@ -239,6 +239,12 @@ function cleanParser(parser) {
parser._consumed = false;
}
function prepareError(err, parser, rawPacket) {
err.rawPacket = rawPacket || parser.getCurrentBuffer();
if (typeof err.reason === 'string')
err.message = `Parse Error: ${err.reason}`;
}
module.exports = {
_checkInvalidHeaderChar: checkInvalidHeaderChar,
_checkIsHttpToken: checkIsHttpToken,
@@ -251,5 +257,6 @@ module.exports = {
methods,
parsers,
kIncomingMessage,
HTTPParser
HTTPParser,
prepareError,
};