mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-10 02:36:29 +00:00
Fix error handling
This commit is contained in:
parent
b486883ff6
commit
e5e25c895f
@ -39,8 +39,8 @@ class Systemd:
|
||||
return self.dbus.Manager.Reboot()
|
||||
|
||||
@dbus_connected
|
||||
def shutdown(self):
|
||||
"""Shutdown host computer.
|
||||
def power_off(self):
|
||||
"""Power off host computer.
|
||||
|
||||
Return a coroutine.
|
||||
"""
|
||||
|
@ -1,6 +1,6 @@
|
||||
"""Utils for dbus."""
|
||||
|
||||
from ..exceptions import HassioNotSupportedError
|
||||
from ..exceptions import DBusNotConnectedError
|
||||
|
||||
|
||||
def dbus_connected(method):
|
||||
@ -8,7 +8,7 @@ def dbus_connected(method):
|
||||
def wrap_dbus(self, *args, **kwargs):
|
||||
"""Check if dbus is connected before call a method."""
|
||||
if self.dbus is None:
|
||||
raise HassioNotSupportedError(f"{self!s} not connected to dbus!")
|
||||
raise DBusNotConnectedError(f"{self!s} not connected to dbus!")
|
||||
return self.method(*args, **kwargs)
|
||||
|
||||
return wrap_dbus
|
||||
|
@ -16,6 +16,18 @@ class HassioNotSupportedError(HassioError):
|
||||
pass
|
||||
|
||||
|
||||
# Host
|
||||
|
||||
class HostError(HassioError):
|
||||
"""Internal Host error."""
|
||||
pass
|
||||
|
||||
|
||||
class HostNotSupportedError(HassioNotSupportedError):
|
||||
"""Host function is not supprted."""
|
||||
pass
|
||||
|
||||
|
||||
# utils/gdbus
|
||||
|
||||
class DBusError(HassioError):
|
||||
@ -23,16 +35,15 @@ class DBusError(HassioError):
|
||||
pass
|
||||
|
||||
|
||||
class DBusNotConnectedError(HassioNotSupportedError):
|
||||
"""DBus is not connected and call a method."""
|
||||
|
||||
|
||||
class DBusFatalError(DBusError):
|
||||
"""DBus call going wrong."""
|
||||
pass
|
||||
|
||||
|
||||
class DBusReturnError(DBusError):
|
||||
"""DBus return error."""
|
||||
pass
|
||||
|
||||
|
||||
class DBusParseError(DBusError):
|
||||
"""DBus parse error."""
|
||||
pass
|
||||
|
@ -1,7 +1,10 @@
|
||||
"""Power control for host."""
|
||||
import logging
|
||||
|
||||
from ..coresys import CoreSysAttributes
|
||||
from ..exceptions import HassioNotSupportedError
|
||||
from ..exceptions import HostNotSupportedError
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PowerControl(CoreSysAttributes):
|
||||
@ -14,11 +17,14 @@ class PowerControl(CoreSysAttributes):
|
||||
def _check_systemd(self):
|
||||
"""Check if systemd is connect or raise error."""
|
||||
if not self.sys_dbus.systemd.is_connected:
|
||||
raise HassioNotSupportedError("No systemd connections")
|
||||
_LOGGER.error("No systemd dbus connection available")
|
||||
raise HostNotSupportedError()
|
||||
|
||||
async def reboot(self):
|
||||
"""Reboot host system."""
|
||||
self._check_systemd()
|
||||
|
||||
_LOGGER.info("Initialize host reboot over systemd")
|
||||
try:
|
||||
await self.sys_core.shutdown()
|
||||
finally:
|
||||
@ -27,7 +33,9 @@ class PowerControl(CoreSysAttributes):
|
||||
async def shutdown(self):
|
||||
"""Shutdown host system."""
|
||||
self._check_systemd()
|
||||
|
||||
_LOGGER.info("Initialize host power off over systemd")
|
||||
try:
|
||||
await self.sys_core.shutdown()
|
||||
finally:
|
||||
await self.sys_dbus.systemd.shutdown()
|
||||
await self.sys_dbus.systemd.power_off()
|
||||
|
@ -4,7 +4,7 @@ import logging
|
||||
import shlex
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from ..exceptions import DBusFatalError, DBusReturnError, DBusParseError
|
||||
from ..exceptions import DBusFatalError, DBusFatalError, DBusParseError
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -47,8 +47,8 @@ class DBus:
|
||||
try:
|
||||
xml = ET.fromstring(data)
|
||||
except ET.ParseError as err:
|
||||
raise DBusParseError(
|
||||
f"Can't parse introspect data: {err!s}") from None
|
||||
_LOGGER.error("Can't parse introspect data: %s", err)
|
||||
raise DBusParseError() from None
|
||||
|
||||
# Read available methods
|
||||
for interface in xml.findall("/node/interface"):
|
||||
@ -91,11 +91,13 @@ class DBus:
|
||||
|
||||
data, _ = await proc.communicate()
|
||||
except OSError as err:
|
||||
raise DBusFatalError(f"DBus fatal error: {err!s}") from None
|
||||
_LOGGER.error("DBus fatal error: %s", err)
|
||||
raise DBusFatalError() from None
|
||||
|
||||
# Success?
|
||||
if proc.returncode != 0:
|
||||
raise DBusReturnError(f"DBus return error: {data!s}")
|
||||
_LOGGER.error("DBus return error: %s", data)
|
||||
raise DBusFatalError()
|
||||
|
||||
# End
|
||||
return data.decode()
|
||||
|
Loading…
x
Reference in New Issue
Block a user