doc: add try/catch in http2 respondWithFile example

PR-URL: https://github.com/nodejs/node/pull/38410
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina 2021-04-26 14:51:16 +02:00 committed by James M Snell
parent 8b603697bc
commit 1adcae96b5
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC

View File

@ -1669,10 +1669,17 @@ server.on('stream', (stream) => {
}
function onError(err) {
if (err.code === 'ENOENT') {
stream.respond({ ':status': 404 });
} else {
stream.respond({ ':status': 500 });
// stream.respond() can throw if the stream has been destroyed by
// the other side.
try {
if (err.code === 'ENOENT') {
stream.respond({ ':status': 404 });
} else {
stream.respond({ ':status': 500 });
}
} catch (err) {
// Perform actual error handling.
console.log(err);
}
stream.end();
}