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