Greg Padgett has uploaded a new change for review. Change subject: client: throw only HA-specific exceptions from client library ......................................................................
client: throw only HA-specific exceptions from client library Callers of the client library need to be able to catch and handle exceptions from the client without them affecting their own operation. To keep them from having to catch (any) Exception, create and propagate an exception class that is HA-specific. Change-Id: I276ea8d5f684be53f174afa0b3c9f70ed377d1a8 Bug-Url: https://bugzilla.redhat.com/?????? Signed-off-by: Greg Padgett <[email protected]> --- M ovirt_hosted_engine_ha/client/client.py M ovirt_hosted_engine_ha/lib/exceptions.py 2 files changed, 42 insertions(+), 9 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-ha refs/changes/07/20307/1 diff --git a/ovirt_hosted_engine_ha/client/client.py b/ovirt_hosted_engine_ha/client/client.py index decfd15..b688a8e 100644 --- a/ovirt_hosted_engine_ha/client/client.py +++ b/ovirt_hosted_engine_ha/client/client.py @@ -19,18 +19,44 @@ import logging import os +import sys import time +from functools import wraps from ..agent import constants as agent_constants from ..env import config from ..env import constants from ..env import path from ..lib import brokerlink +from ..lib import exceptions from ..lib import metadata -from ..lib.exceptions import MetadataError + + +# TODO When time permits, it would be better for the client APIs to +# return meaningful exceptions rather than the generic HAException. + +# Allow callers to refer to client.HAException rather than +# forcing them to import our lib.exceptions module directly +HAException = exceptions.HAException + + +def ha_exceptions(f): + """ + Decorator to raise only HAException from decorated methods + """ + def _ha_exceptions(*args, **kwargs): + try: + return f(*args, **kwargs) + except Exception as e: + if isinstance(e, HAException): + raise + else: + raise HAException(str(e)), None, sys.exc_info()[2] + return wraps(f)(_ha_exceptions) class HAClient(object): + @ha_exceptions def __init__(self, log=False): """ Create an instance of HAClient. If the caller has a log handler, it @@ -42,6 +68,7 @@ self._log = logging.getLogger("HAClient") self._config = None + @ha_exceptions def get_all_host_stats(self): """ Connects to HA broker, reads stats for all hosts, and returns @@ -59,13 +86,14 @@ for host_id, data in stats.iteritems(): try: md = metadata.parse_metadata_to_dict(host_id, data) - except MetadataError as e: + except exceptions.MetadataError as e: self._log.error(str(e)) continue else: output[host_id] = md return output + @ha_exceptions def get_all_host_stats_direct(self, dom_path, service_type): """ Connects to HA broker, reads stats for all hosts, and returns @@ -81,13 +109,14 @@ for host_id, data in stats.iteritems(): try: md = metadata.parse_metadata_to_dict(host_id, data) - except MetadataError as e: + except exceptions.MetadataError as e: self._log.error(str(e)) continue else: output[host_id] = md return output + @ha_exceptions def get_local_host_score(self): if self._config is None: self._config = config.Config() @@ -103,7 +132,7 @@ if host_id in stats: try: md = metadata.parse_metadata_to_dict(host_id, stats[host_id]) - except MetadataError as e: + except exceptions.MetadataError as e: self._log.error(str(e)) else: # Only report a non-zero score if the local host has had a diff --git a/ovirt_hosted_engine_ha/lib/exceptions.py b/ovirt_hosted_engine_ha/lib/exceptions.py index c5563c0..8e58a36 100644 --- a/ovirt_hosted_engine_ha/lib/exceptions.py +++ b/ovirt_hosted_engine_ha/lib/exceptions.py @@ -22,22 +22,26 @@ """ -class DisconnectionError(Exception): +class HAException(Exception): pass -class RequestError(Exception): +class DisconnectionError(HAException): pass -class DetailedError(Exception): +class RequestError(HAException): + pass + + +class DetailedError(HAException): """ Allows testing for detailed conditions while preserving clean str(e) """ def __init__(self, msg, detail): - Exception.__init__(self, msg) + HAException.__init__(self, msg) self.detail = detail -class MetadataError(Exception): +class MetadataError(HAException): pass -- To view, visit http://gerrit.ovirt.org/20307 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I276ea8d5f684be53f174afa0b3c9f70ed377d1a8 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-hosted-engine-ha Gerrit-Branch: master Gerrit-Owner: Greg Padgett <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
