mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-13 12:16:29 +00:00
Handle exception chain on addon boot (#2063)
* Handle exception chain on addon boot * fix import * Add tests
This commit is contained in:
parent
7633d26806
commit
78f63380f2
@ -15,11 +15,11 @@ from ..exceptions import (
|
|||||||
DockerAPIError,
|
DockerAPIError,
|
||||||
DockerError,
|
DockerError,
|
||||||
DockerNotFound,
|
DockerNotFound,
|
||||||
DockerRequestError,
|
|
||||||
HomeAssistantAPIError,
|
HomeAssistantAPIError,
|
||||||
HostAppArmorError,
|
HostAppArmorError,
|
||||||
)
|
)
|
||||||
from ..store.addon import AddonStore
|
from ..store.addon import AddonStore
|
||||||
|
from ..utils import check_exception_chain
|
||||||
from .addon import Addon
|
from .addon import Addon
|
||||||
from .data import AddonsData
|
from .data import AddonsData
|
||||||
|
|
||||||
@ -102,11 +102,13 @@ class AddonManager(CoreSysAttributes):
|
|||||||
for addon in tasks:
|
for addon in tasks:
|
||||||
try:
|
try:
|
||||||
await addon.start()
|
await addon.start()
|
||||||
except DockerRequestError:
|
except AddonsError as err:
|
||||||
pass
|
# Check if there is an system/user issue
|
||||||
except (AddonConfigurationError, DockerAPIError, DockerNotFound):
|
if check_exception_chain(
|
||||||
addon.boot = AddonBoot.MANUAL
|
err, (DockerAPIError, DockerNotFound, AddonConfigurationError)
|
||||||
addon.save_persist()
|
):
|
||||||
|
addon.boot = AddonBoot.MANUAL
|
||||||
|
addon.save_persist()
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # pylint: disable=broad-except
|
||||||
self.sys_capture_exception(err)
|
self.sys_capture_exception(err)
|
||||||
else:
|
else:
|
||||||
|
@ -5,7 +5,7 @@ from ipaddress import IPv4Address
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
from typing import Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -107,3 +107,17 @@ def check_port(address: IPv4Address, port: int) -> bool:
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_exception_chain(err: Exception, object_type: Any) -> bool:
|
||||||
|
"""Check if exception chain include sub exception.
|
||||||
|
|
||||||
|
It's not full recursive because we need mostly only access to the latest.
|
||||||
|
"""
|
||||||
|
if issubclass(type(err), object_type):
|
||||||
|
return True
|
||||||
|
|
||||||
|
if not err.__context__:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return check_exception_chain(err.__context__, object_type)
|
||||||
|
36
tests/utils/test_exception_helper.py
Normal file
36
tests/utils/test_exception_helper.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""Test exception helpers."""
|
||||||
|
|
||||||
|
from supervisor.utils import check_exception_chain
|
||||||
|
|
||||||
|
|
||||||
|
def test_simple_chain_exception():
|
||||||
|
"""Test simple chain of excepiton."""
|
||||||
|
|
||||||
|
try:
|
||||||
|
raise ValueError()
|
||||||
|
except ValueError as err:
|
||||||
|
assert check_exception_chain(err, ValueError)
|
||||||
|
|
||||||
|
|
||||||
|
def test_simple_nested_chain_exception():
|
||||||
|
"""Test simple nested chain of excepiton."""
|
||||||
|
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
raise ValueError()
|
||||||
|
except ValueError as err:
|
||||||
|
raise KeyError() from err
|
||||||
|
except KeyError as err:
|
||||||
|
assert check_exception_chain(err, ValueError)
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_nested_chain_exception():
|
||||||
|
"""Test list nested chain of excepiton."""
|
||||||
|
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
raise ValueError()
|
||||||
|
except ValueError as err:
|
||||||
|
raise KeyError() from err
|
||||||
|
except KeyError as err:
|
||||||
|
assert check_exception_chain(err, (ValueError, OSError))
|
Loading…
x
Reference in New Issue
Block a user