Skip to content

Commit

Permalink
handle errors in dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
danprince committed Jun 11, 2020
1 parent 23032aa commit d0a7e41
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/commands/dev.ts
Expand Up @@ -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!);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d0a7e41

Please sign in to comment.