From d0a7e41d767544f9eaefe7b1824a1f9eb65a0d48 Mon Sep 17 00:00:00 2001 From: danprince Date: Fri, 12 Jun 2020 00:19:27 +0100 Subject: [PATCH] handle errors in dev server --- src/commands/dev.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/commands/dev.ts b/src/commands/dev.ts index 6521081ad9..d058958481 100644 --- a/src/commands/dev.ts +++ b/src/commands/dev.ts @@ -462,12 +462,7 @@ export async function command(commandOptions: CommandOptions) { } } - const createServer = credentials - ? (requestHandler) => - http2.createSecureServer({...credentials!, allowHTTP1: true}, requestHandler) - : (requestHandler) => http.createServer(requestHandler); - - const server = createServer(async (req, res) => { + async function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) { const reqUrl = req.url!; const reqUrlHmrParam = reqUrl.includes('?mtime=') && reqUrl.split('?')[1]; let reqPath = decodeURI(url.parse(reqUrl).pathname!); @@ -800,6 +795,20 @@ export async function command(commandOptions: CommandOptions) { } sendFile(req, res, wrappedResponse, responseFileExt); + } + + const createServer = credentials + ? (requestHandler) => + http2.createSecureServer({...credentials!, allowHTTP1: true}, requestHandler) + : (requestHandler) => http.createServer(requestHandler); + + const server = createServer(async (req, res) => { + try { + return await requestHandler(req, res); + } catch (err) { + console.error(`[500] ${req.url}\n${err.message}`); + return sendError(res, 500); + } }) .on('error', (err: Error) => { console.error(chalk.red(` ✘ Failed to start server at port ${chalk.bold(port)}.`), err);