From 4ff19c9fa5863cc8f8da6f9a0cd6a6625af417b0 Mon Sep 17 00:00:00 2001 From: CountBleck Date: Tue, 30 Aug 2022 15:24:48 -0700 Subject: [PATCH] Improve error handling in some cases In Cyclone's fetchBare(), the content type wasn't correct, and the status line should be "Internal Server Error", not just "Error". In the main request handler, I added similar error handling, just in case. --- app.js | 19 +++++++++++++------ static/customBare.mjs | 4 ++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index e73bc69..499ef6e 100644 --- a/app.js +++ b/app.js @@ -19,12 +19,19 @@ const serve = new nodeStatic.Server(join( const server = http.createServer(); server.on('request', (request, response) => { - if (custombare.route(request, response)) return true; - - if (bareServer.shouldRoute(request)) { - bareServer.routeRequest(request, response); - } else { - serve.serve(request, response); + try { + if (custombare.route(request, response)) return true; + + if (bareServer.shouldRoute(request)) { + bareServer.routeRequest(request, response); + } else { + serve.serve(request, response); + } + } catch (e) { + response.writeHead(500, "Internal Server Error", { + "Content-Type": "text/plain" + }) + response.end(e.stack) } }); server.on('upgrade', (req, socket, head) => { diff --git a/static/customBare.mjs b/static/customBare.mjs index 91afad7..bc14db5 100644 --- a/static/customBare.mjs +++ b/static/customBare.mjs @@ -82,8 +82,8 @@ async function fetchBare(url, res, req) { request.body.pipe(res) } } catch (e) { - res.writeHead(500, 'Error', { - 'content-type': 'application/javascript' + res.writeHead(500, 'Internal Server Error', { + 'Content-Type': 'text/plain' }) res.end(e.stack); }