From eff9793b331687360d1db1d429d7935b6f692bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 27 Jan 2021 17:41:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=91=20Better=20error=20handling=20for?= =?UTF-8?q?=20a=20python=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Non-ASCII host names under Windows are bad news for firing up an HTTPServer instance. We can't fix that, but we can display a better error message. See https://bugs.python.org/issue26227 Closes #3963 --- src/octoprint/cli/server.py | 13 +++++++++++-- src/octoprint/server/__init__.py | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/octoprint/cli/server.py b/src/octoprint/cli/server.py index eff6f5c19..2788cd5fc 100644 --- a/src/octoprint/cli/server.py +++ b/src/octoprint/cli/server.py @@ -143,7 +143,7 @@ def run_server( method("There was a fatal error starting up OctoPrint.") else: - from octoprint.server import Server + from octoprint.server import CannotStartServerException, Server octoprint_server = Server( settings=settings, @@ -159,7 +159,16 @@ def run_server( allow_root=allow_root, octoprint_daemon=octoprint_daemon, ) - octoprint_server.run() + + try: + octoprint_server.run() + except CannotStartServerException as e: + logger = logging.getLogger("octoprint.startup").fatal + echo = lambda x: click.echo(x, err=True) + + for method in logger, echo: + method(str(e)) + method("There was a fatal error starting up OctoPrint.") # ~~ server options diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index d4bbdad90..cce79ca2e 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -2441,9 +2441,25 @@ class Server(object): try: self._intermediary_server.server_bind() self._intermediary_server.server_activate() - except Exception: + except Exception as exc: self._intermediary_server.server_close() - raise + + if isinstance(exc, UnicodeDecodeError) and sys.platform == "win32": + # we end up here if the hostname contains non-ASCII characters due to + # https://bugs.python.org/issue26227 - tell the user they need + # to either change their hostname or read up other options in + # https://github.com/OctoPrint/OctoPrint/issues/3963 + raise CannotStartServerException( + "OctoPrint cannot start due to a Python bug " + "(https://bugs.python.org/issue26227). Your " + "computer's host name contains non-ASCII characters. " + "Please either change your computer's host name to " + "contain only ASCII characters, or take a look at " + "https://github.com/OctoPrint/OctoPrint/issues/3963 for " + "other options." + ) + else: + raise def serve(): try: @@ -2656,3 +2672,7 @@ class LifecycleManager(object): for event in events: if callback in self._plugin_lifecycle_callbacks[event]: self._plugin_lifecycle_callbacks[event].remove(callback) + + +class CannotStartServerException(Exception): + pass