mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-09 10:16:29 +00:00
Format API error messages (#1997)
This commit is contained in:
parent
928a4d8dce
commit
f32d17d924
@ -552,7 +552,7 @@ class Addon(AddonModel):
|
|||||||
await self.instance.run()
|
await self.instance.run()
|
||||||
except DockerAPIError as err:
|
except DockerAPIError as err:
|
||||||
self.state = AddonState.ERROR
|
self.state = AddonState.ERROR
|
||||||
raise AddonsError() from err
|
raise AddonsError(err) from None
|
||||||
else:
|
else:
|
||||||
self.state = AddonState.STARTED
|
self.state = AddonState.STARTED
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
"""Init file for Supervisor util for RESTful API."""
|
"""Init file for Supervisor util for RESTful API."""
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -19,8 +18,7 @@ from ..const import (
|
|||||||
RESULT_OK,
|
RESULT_OK,
|
||||||
)
|
)
|
||||||
from ..exceptions import APIError, APIForbidden, HassioError
|
from ..exceptions import APIError, APIForbidden, HassioError
|
||||||
|
from ..utils.log_format import format_message
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def excract_supervisor_token(request: web.Request) -> Optional[str]:
|
def excract_supervisor_token(request: web.Request) -> Optional[str]:
|
||||||
@ -61,8 +59,10 @@ def api_process(method):
|
|||||||
answer = await method(api, *args, **kwargs)
|
answer = await method(api, *args, **kwargs)
|
||||||
except (APIError, APIForbidden) as err:
|
except (APIError, APIForbidden) as err:
|
||||||
return api_return_error(message=str(err))
|
return api_return_error(message=str(err))
|
||||||
except HassioError:
|
except HassioError as err:
|
||||||
return api_return_error(message="Unknown Error, see logs")
|
return api_return_error(
|
||||||
|
message=str(err) if err else "Unknown Error, see logs"
|
||||||
|
)
|
||||||
|
|
||||||
if isinstance(answer, dict):
|
if isinstance(answer, dict):
|
||||||
return api_return_ok(data=answer)
|
return api_return_ok(data=answer)
|
||||||
@ -103,7 +103,7 @@ def api_process_raw(content):
|
|||||||
def api_return_error(message: Optional[str] = None) -> web.Response:
|
def api_return_error(message: Optional[str] = None) -> web.Response:
|
||||||
"""Return an API error message."""
|
"""Return an API error message."""
|
||||||
return web.json_response(
|
return web.json_response(
|
||||||
{JSON_RESULT: RESULT_ERROR, JSON_MESSAGE: message}, status=400
|
{JSON_RESULT: RESULT_ERROR, JSON_MESSAGE: format_message(message)}, status=400
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ class DockerAPI:
|
|||||||
container.start()
|
container.start()
|
||||||
except (docker.errors.DockerException, requests.RequestException) as err:
|
except (docker.errors.DockerException, requests.RequestException) as err:
|
||||||
_LOGGER.error("Can't start %s: %s", name, err)
|
_LOGGER.error("Can't start %s: %s", name, err)
|
||||||
raise DockerAPIError() from err
|
raise DockerAPIError(err) from None
|
||||||
|
|
||||||
# Update metadata
|
# Update metadata
|
||||||
with suppress(docker.errors.DockerException, requests.RequestException):
|
with suppress(docker.errors.DockerException, requests.RequestException):
|
||||||
|
15
supervisor/utils/log_format.py
Normal file
15
supervisor/utils/log_format.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
"""Custom log messages."""
|
||||||
|
import re
|
||||||
|
|
||||||
|
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."
|
||||||
|
)
|
||||||
|
|
||||||
|
return message
|
11
tests/utils/test_log_format.py
Normal file
11
tests/utils/test_log_format.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
"""Tests for message formater."""
|
||||||
|
from supervisor.utils.log_format import format_message
|
||||||
|
|
||||||
|
|
||||||
|
def test_format_message():
|
||||||
|
"""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)
|
||||||
|
== "Port '80' is already in use by something else on the host."
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user