From c9db42583b39f1bfe5d0cf46c7bb87c9c45f1f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 14 Sep 2020 13:40:11 +0200 Subject: [PATCH] Capture typeerror exception (#2052) --- supervisor/utils/log_format.py | 17 ++++++++++++----- tests/utils/test_log_format.py | 6 ++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/supervisor/utils/log_format.py b/supervisor/utils/log_format.py index c55d4e66d..01d6cb61a 100644 --- a/supervisor/utils/log_format.py +++ b/supervisor/utils/log_format.py @@ -1,15 +1,22 @@ """Custom log messages.""" +import logging import re +import sentry_sdk + +_LOGGER: logging.Logger = logging.getLogger(__name__) + RE_BIND_FAILED = re.compile(r".*Bind for.*:(\d*) failed: port is already allocated.*") def format_message(message: str) -> str: """Return a formated message if it's known.""" - match = RE_BIND_FAILED.match(message) - if match: - return ( - f"Port '{match.group(1)}' is already in use by something else on the host." - ) + try: + match = RE_BIND_FAILED.match(message) + if match: + return f"Port '{match.group(1)}' is already in use by something else on the host." + except TypeError as err: + _LOGGER.error("Type of message is not string - %s", err) + sentry_sdk.capture_exception(err) return message diff --git a/tests/utils/test_log_format.py b/tests/utils/test_log_format.py index d03e22849..7225f0762 100644 --- a/tests/utils/test_log_format.py +++ b/tests/utils/test_log_format.py @@ -9,3 +9,9 @@ def test_format_message(): format_message(message) == "Port '80' is already in use by something else on the host." ) + + +def test_exeption(): + """Tests the exception handling.""" + message = b"byte" + assert format_message(message) == message