From 0786e06eb954e203c3adaf1c75341a95f2bb356f Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Sat, 1 Mar 2025 15:52:29 +0000 Subject: [PATCH] Re-add typeerror handling to format message --- supervisor/api/utils.py | 4 ++-- supervisor/utils/log_format.py | 22 +++++++++++++++------- tests/utils/test_log_format.py | 12 +++++++++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/supervisor/api/utils.py b/supervisor/api/utils.py index 1dc4b2781..a3195d595 100644 --- a/supervisor/api/utils.py +++ b/supervisor/api/utils.py @@ -25,7 +25,7 @@ from ..coresys import CoreSys from ..exceptions import APIError, BackupFileNotFoundError, DockerAPIError, HassioError from ..utils import check_exception_chain, get_message_from_exception_chain from ..utils.json import json_dumps, json_loads as json_loads_util -from ..utils.log_format import format_message +from ..utils.log_format import async_format_message from . import const @@ -139,7 +139,7 @@ def api_return_error( if error and not message: message = get_message_from_exception_chain(error) if check_exception_chain(error, DockerAPIError): - message = format_message(message) + message = async_format_message(message) if not message: message = "Unknown error, see supervisor" diff --git a/supervisor/utils/log_format.py b/supervisor/utils/log_format.py index 503f8d6ef..e8cec3057 100644 --- a/supervisor/utils/log_format.py +++ b/supervisor/utils/log_format.py @@ -1,8 +1,11 @@ """Custom log messages.""" +import asyncio import logging import re +from .sentry import async_capture_exception + _LOGGER: logging.Logger = logging.getLogger(__name__) RE_BIND_FAILED = re.compile( @@ -10,12 +13,17 @@ RE_BIND_FAILED = re.compile( ) -def format_message(message: str) -> str: - """Return a formatted 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." - ) +def async_format_message(message: str) -> str: + """Return a formated message if it's known. + + Must be called from event loop. + """ + 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("The type of message is not a string - %s", err) + asyncio.get_running_loop().create_task(async_capture_exception(err)) return message diff --git a/tests/utils/test_log_format.py b/tests/utils/test_log_format.py index be1f90462..25eba5e55 100644 --- a/tests/utils/test_log_format.py +++ b/tests/utils/test_log_format.py @@ -1,13 +1,13 @@ """Tests for message formater.""" -from supervisor.utils.log_format import format_message +from supervisor.utils.log_format import async_format_message def test_format_message_port(): """Tests for message formater.""" message = '500 Server Error: Internal Server Error: Bind for 0.0.0.0:80 failed: port is already allocated")' assert ( - format_message(message) + async_format_message(message) == "Port '80' is already in use by something else on the host." ) @@ -16,6 +16,12 @@ def test_format_message_port_alternative(): """Tests for message formater.""" message = 'Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use")' assert ( - format_message(message) + async_format_message(message) == "Port '80' is already in use by something else on the host." ) + + +async def test_exeption(): + """Tests the exception handling.""" + message = b"byte" + assert async_format_message(message) == message