From 05fa19380de01565245ba46475a0aecaa8141a89 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 --- 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 fa7c666e87436b..c78223e9541361 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -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(); }