From 3c6d90a4db0431c642792a7f743b2c89ecdc5865 Mon Sep 17 00:00:00 2001 From: bmccandless Date: Thu, 16 Jul 2020 11:08:27 -0700 Subject: [PATCH] Better error checking on bad params (#1626) --- server/common/app_config.py | 24 ++++++++++++++++++------ server/test/__init__.py | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/server/common/app_config.py b/server/common/app_config.py index 38b47fc2..570b58ae 100644 --- a/server/common/app_config.py +++ b/server/common/app_config.py @@ -17,7 +17,7 @@ from server.common.utils import custom_format_warning import server.compute.diffexp_cxg as diffexp_tiledb from server.common.data_locator import discover_s3_region_name -DEFAULT_SERVER_PORT = int(os.environ.get("CXG_SERVER_PORT", "5005")) +DEFAULT_SERVER_PORT = 5005 # anything bigger than this will generate a special message BIG_FILE_SIZE_THRESHOLD = 100 * 2 ** 20 # 100MB @@ -438,12 +438,24 @@ class ServerConfig(BaseConfig): self.check_attr("app__csp_directives", (type(None), dict)) if self.app__port: - if not is_port_available(self.app__host, self.app__port): - raise ConfigurationError( - f"The port selected {self.app__port} is in use, please configure an open port." - ) + try: + if not is_port_available(self.app__host, self.app__port): + raise ConfigurationError( + f"The port selected {self.app__port} is in use, please configure an open port." + ) + except OverflowError: + raise ConfigurationError(f"Invalid port: {self.app__port}") else: - self.app__port = find_available_port(self.app__host, DEFAULT_SERVER_PORT) + try: + default_server_port = int(os.environ.get("CXG_SERVER_PORT", DEFAULT_SERVER_PORT)) + except ValueError: + raise ConfigurationError( + "Invalid port from environment variable CXG_SERVER_PORT: " + os.environ.get("CXG_SERVER_PORT") + ) + try: + self.app__port = find_available_port(self.app__host, default_server_port) + except OverflowError: + raise ConfigurationError(f"Invalid port: {default_server_port}") if self.app__debug: context["messagefn"]("in debug mode, setting verbose=True and open_browser=False") diff --git a/server/test/__init__.py b/server/test/__init__.py index 48d999ba..bc82be90 100644 --- a/server/test/__init__.py +++ b/server/test/__init__.py @@ -104,7 +104,7 @@ def test_server(command_line_args=[], app_config=None): yaml config file, which this server will read and parse. """ - port = DEFAULT_SERVER_PORT + port = int(os.environ.get("CXG_SERVER_PORT", DEFAULT_SERVER_PORT)) port = find_available_port("localhost", port) command = ["cellxgene", "--no-upgrade-check", "launch", "--verbose", "--port=%d" % port] + command_line_args