Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract exception message from chain for API errors #2100

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion supervisor/addons/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ async def start(self) -> None:
raise AddonsError() from err
except DockerError as err:
self.state = AddonState.ERROR
raise AddonsError(err) from err
raise AddonsError() from err
else:
self.state = AddonState.STARTED

Expand Down
22 changes: 13 additions & 9 deletions supervisor/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
RESULT_ERROR,
RESULT_OK,
)
from ..exceptions import APIError, APIForbidden, HassioError
from ..exceptions import APIError, APIForbidden, DockerAPIError, HassioError
from ..utils import check_exception_chain, get_message_from_exception_chain
from ..utils.log_format import format_message


Expand Down Expand Up @@ -57,12 +58,8 @@ async def wrap_api(api, *args, **kwargs):
"""Return API information."""
try:
answer = await method(api, *args, **kwargs)
except (APIError, APIForbidden) as err:
return api_return_error(message=str(err))
except HassioError as err:
return api_return_error(
message=str(err) if err else "Unknown Error, see logs"
)
except (APIError, APIForbidden, HassioError) as err:
return api_return_error(error=err)

if isinstance(answer, dict):
return api_return_ok(data=answer)
Expand Down Expand Up @@ -100,10 +97,17 @@ async def wrap_api(api, *args, **kwargs):
return wrap_method


def api_return_error(message: Optional[str] = None) -> web.Response:
def api_return_error(error: Optional[Any] = None) -> web.Response:
"""Return an API error message."""
message = get_message_from_exception_chain(error)
if check_exception_chain(error, DockerAPIError):
message = format_message(message)
return web.json_response(
{JSON_RESULT: RESULT_ERROR, JSON_MESSAGE: format_message(message)}, status=400
{
JSON_RESULT: RESULT_ERROR,
JSON_MESSAGE: message or "Unknown error, see supervisor",
},
status=400,
)


Expand Down
11 changes: 11 additions & 0 deletions supervisor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ def check_exception_chain(err: Exception, object_type: Any) -> bool:
return False

return check_exception_chain(err.__context__, object_type)


def get_message_from_exception_chain(err: Exception) -> str:
"""Get the first message from the exception chain."""
if str(err):
return str(err)

if not err.__context__:
return ""

return get_message_from_exception_chain(err.__context__)
32 changes: 31 additions & 1 deletion tests/utils/test_exception_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test exception helpers."""

from supervisor.utils import check_exception_chain
from supervisor.utils import check_exception_chain, get_message_from_exception_chain


def test_simple_chain_exception():
Expand Down Expand Up @@ -55,3 +55,33 @@ def test_list_nested_chain_exception_not():
raise KeyError() from err
except KeyError as err:
assert not check_exception_chain(err, (AssertionError, OSError))


def test_simple_chain_exception_message():
"""Test simple chain of excepiton."""

try:
raise ValueError("error")
except ValueError as err:
assert get_message_from_exception_chain(err) == "error"


def test_simple_chain_exception_not_message():
"""Test simple chain of excepiton."""

try:
raise ValueError()
except ValueError as err:
assert not get_message_from_exception_chain(err)


def test_simple_nested_chain_exception_message():
"""Test simple nested chain of excepiton."""

try:
try:
raise ValueError("error")
except ValueError as err:
raise KeyError() from err
except KeyError as err:
assert get_message_from_exception_chain(err) == "error"