Fix error handing (#82)

Add an optional extended description…
This commit is contained in:
Pascal Vizeli 2017-06-26 09:17:09 +02:00 committed by GitHub
parent 1b887e38d6
commit 40343089b5
5 changed files with 38 additions and 29 deletions

6
API.md
View File

@ -78,10 +78,10 @@ Get all available addons
"repositories": [ "repositories": [
{ {
"slug": "12345678", "slug": "12345678",
"name": "Repitory Name", "name": "Repitory Name|unknown",
"source": "URL_OF_REPOSITORY", "source": "URL_OF_REPOSITORY",
"url": "null|WEBSITE", "url": "WEBSITE|REPOSITORY",
"maintainer": "null|BLA BLU <fla@dld.ch>" "maintainer": "BLA BLU <fla@dld.ch>|unknown"
} }
] ]
} }

View File

@ -260,12 +260,13 @@ class Addon(object):
async def install(self, version=None): async def install(self, version=None):
"""Install a addon.""" """Install a addon."""
if self.config.arch not in self.supported_arch: if self.config.arch not in self.supported_arch:
raise RuntimeError("Addon {} not supported on {}".format( _LOGGER.error(
self._id, self.config.arch)) "Addon %s not supported on %s", self._id, self.config.arch)
return False
if self.is_installed: if self.is_installed:
raise RuntimeError( _LOGGER.error("Addon %s is already installed", self._id)
"Addon {} is already installed".format(self._id)) return False
if not self.path_data.is_dir(): if not self.path_data.is_dir():
_LOGGER.info( _LOGGER.info(
@ -282,7 +283,8 @@ class Addon(object):
async def uninstall(self): async def uninstall(self):
"""Remove a addon.""" """Remove a addon."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
if not await self.addon_docker.remove(): if not await self.addon_docker.remove():
return False return False
@ -307,29 +309,33 @@ class Addon(object):
async def start(self): async def start(self):
"""Set options and start addon.""" """Set options and start addon."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
if not self.write_addon_options(): if not self.write_addon_options():
raise RuntimeError( return False
"Can't write options for addon {}".format(self._id))
return await self.addon_docker.run() return await self.addon_docker.run()
async def stop(self): async def stop(self):
"""Stop addon.""" """Stop addon."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
return await self.addon_docker.stop() return await self.addon_docker.stop()
async def update(self, version=None): async def update(self, version=None):
"""Update addon.""" """Update addon."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
version = version or self.last_version version = version or self.last_version
if version == self.version_installed: if version == self.version_installed:
raise RuntimeError("Version is already in use") _LOGGER.warning(
"Addon %s is already installed in %s", self._id, version)
return True
if not await self.addon_docker.update(version): if not await self.addon_docker.update(version):
return False return False
@ -340,17 +346,18 @@ class Addon(object):
async def restart(self): async def restart(self):
"""Restart addon.""" """Restart addon."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
if not self.write_addon_options(): if not self.write_addon_options():
raise RuntimeError( return False
"Can't write options for addon {}".format(self._id))
return await self.addon_docker.restart() return await self.addon_docker.restart()
async def logs(self): async def logs(self):
"""Return addons log output.""" """Return addons log output."""
if not self.is_installed: if not self.is_installed:
raise RuntimeError("Addon {} is not installed".format(self._id)) _LOGGER.error("Addon %s is not installed", self._id)
return False
return await self.addon_docker.logs() return await self.addon_docker.logs()

View File

@ -4,6 +4,8 @@ from .util import get_hash_from_repository
from ..const import ( from ..const import (
REPOSITORY_CORE, REPOSITORY_LOCAL, ATTR_NAME, ATTR_URL, ATTR_MAINTAINER) REPOSITORY_CORE, REPOSITORY_LOCAL, ATTR_NAME, ATTR_URL, ATTR_MAINTAINER)
UNKNOWN = 'unknown'
class Repository(object): class Repository(object):
"""Repository in HassIO.""" """Repository in HassIO."""
@ -37,17 +39,17 @@ class Repository(object):
@property @property
def name(self): def name(self):
"""Return name of repository.""" """Return name of repository."""
return self._mesh.get(ATTR_NAME, self.source) return self._mesh.get(ATTR_NAME, UNKNOWN)
@property @property
def url(self): def url(self):
"""Return url of repository.""" """Return url of repository."""
return self._mesh.get(ATTR_URL) return self._mesh.get(ATTR_URL, self.source)
@property @property
def maintainer(self): def maintainer(self):
"""Return url of repository.""" """Return url of repository."""
return self._mesh.get(ATTR_MAINTAINER) return self._mesh.get(ATTR_MAINTAINER, UNKNOWN)
async def load(self): async def load(self):
"""Load addon repository.""" """Load addon repository."""

View File

@ -30,18 +30,21 @@ class APIAddons(object):
self.loop = loop self.loop = loop
self.addons = addons self.addons = addons
def _extract_addon(self, request): def _extract_addon(self, request, check_installed=True):
"""Return addon and if not exists trow a exception.""" """Return addon and if not exists trow a exception."""
addon = self.addons.get(request.match_info.get('addon')) addon = self.addons.get(request.match_info.get('addon'))
if not addon: if not addon:
raise RuntimeError("Addon not exists") raise RuntimeError("Addon not exists")
if check_installed and not addon.is_installed:
raise RuntimeError("Addon is not installed")
return addon return addon
@api_process @api_process
async def info(self, request): async def info(self, request):
"""Return addon information.""" """Return addon information."""
addon = self._extract_addon(request) addon = self._extract_addon(request, check_installed=False)
return { return {
ATTR_NAME: addon.name, ATTR_NAME: addon.name,
@ -62,9 +65,6 @@ class APIAddons(object):
"""Store user options for addon.""" """Store user options for addon."""
addon = self._extract_addon(request) addon = self._extract_addon(request)
if not addon.is_installed:
raise RuntimeError("Addon {} is not installed!".format(addon.slug))
addon_schema = SCHEMA_OPTIONS.extend({ addon_schema = SCHEMA_OPTIONS.extend({
vol.Optional(ATTR_OPTIONS): addon.schema, vol.Optional(ATTR_OPTIONS): addon.schema,
}) })
@ -79,7 +79,7 @@ class APIAddons(object):
return True return True
@api_process @api_process
async def install(self, request): async def install(self, request, check_installed=False):
"""Install addon.""" """Install addon."""
body = await api_validate(SCHEMA_VERSION, request) body = await api_validate(SCHEMA_VERSION, request)
addon = self._extract_addon(request) addon = self._extract_addon(request)

View File

@ -189,13 +189,13 @@ class DockerBase(object):
except docker.errors.DockerException: except docker.errors.DockerException:
return return
_LOGGER.info("Stop %s docker application", self.image)
if container.status == 'running': if container.status == 'running':
_LOGGER.info("Stop %s docker application", self.image)
with suppress(docker.errors.DockerException): with suppress(docker.errors.DockerException):
container.stop(timeout=15) container.stop(timeout=15)
with suppress(docker.errors.DockerException): with suppress(docker.errors.DockerException):
_LOGGER.info("Clean %s docker application", self.image)
container.remove(force=True) container.remove(force=True)
async def remove(self): async def remove(self):