Re-add typeerror handling to format message

This commit is contained in:
Mike Degatano 2025-03-01 15:52:29 +00:00
parent 5b18fb6b12
commit 0786e06eb9
3 changed files with 26 additions and 12 deletions

View File

@ -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"

View File

@ -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

View File

@ -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