From 00943affea6ae557a21e8ba6fc2637b26c7e5755 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Tue, 18 Feb 2020 11:34:21 -0800 Subject: [PATCH] Add friendly EADDRINUSE error message Problem: When the port was already taken by another process we've been dumping a big esoteric error message that really isn't helpful for most people. Solution: Catch the error and give our users a friendly error message with a suggestion on how to run Oasis on another port. --- src/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/index.js b/src/index.js index efac986..f967764 100755 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,24 @@ if (config.debug) { process.env.DEBUG = "oasis,oasis:*"; } +process.on("uncaughtException", function(err) { + // This isn't `err.code` because TypeScript doesn't like that. + if (err["code"] === "EADDRINUSE") { + const url = `http://${config.host}:${config.port}`; + + throw new Error( + `Another server is already running at ${url}. +It might be another copy of Oasis or another program on your computer. +You can run Oasis on a different port number with this option: + + oasis --port ${config.port + 1} +` + ); + } else { + throw err; + } +}); + // HACK: We must get the CLI config and then delete environment variables. // This hides arguments from other upstream modules who might parse them. //