From 217515344e43c17d38ddb925ef6b0d3a55765ac5 Mon Sep 17 00:00:00 2001 From: John McCambridge Date: Wed, 3 Apr 2019 15:50:52 -0500 Subject: [PATCH] Add port in use message (#418) * Add clear error message if port is in use * Add bold function for text/numbers * remove unused dependency: * remove unused line break * Change logger message * Use NodeJS.ErrnoException type * Add back check for error code --- packages/server/src/cli.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/server/src/cli.ts b/packages/server/src/cli.ts index 1b077a1a5..d2a3505b5 100644 --- a/packages/server/src/cli.ts +++ b/packages/server/src/cli.ts @@ -38,6 +38,10 @@ Error.stackTraceLimit = Infinity; if (isCli) { require("nbin").shimNativeFs(buildDir); } +// Makes strings or numbers bold in stdout +const bold = (text: string | number): string | number => { + return `\u001B[1m${text}\u001B[0m`; +}; (async (): Promise => { const args = commander.args; @@ -234,7 +238,12 @@ if (isCli) { logger.info(`WebSocket closed \u001B[0m${req.url}`, field("client", id), field("code", code)); }); }); - + app.wss.on("error", (err: NodeJS.ErrnoException) => { + if (err.code === "EADDRINUSE") { + logger.error(`Port ${bold(options.port)} is in use. Please free up port ${options.port} or specify a different port with the -p flag`); + process.exit(1); + } + }); if (!options.certKey && !options.cert) { logger.warn("No certificate specified. \u001B[1mThis could be insecure."); // TODO: fill in appropriate doc url