From 945450b5cf1ac7bfaa998984ef6d6e05403d16b8 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 26 Apr 2021 14:51:16 +0200 Subject: [PATCH] doc: add try/catch in http2 respondWithFile example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/38410 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Gerhard Stöbich Reviewed-By: James M Snell --- doc/api/http2.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/doc/api/http2.md b/doc/api/http2.md index 623e6a96071cb7..fd83a711313c8e 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -1636,10 +1636,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(); }