Rely on inheritance to handle errors in Result and Pod.

This commit is contained in:
Jason R. Coombs 2016-09-03 15:36:52 -04:00
parent f0fc35c7c1
commit 7bee559740

View File

@ -56,16 +56,16 @@ class Client(object):
class ErrorHandler(object): class ErrorHandler(object):
@staticmethod def __init__(self, *args, **kwargs):
def _handle_error(resp): super(ErrorHandler, self).__init__(*args, **kwargs)
error = resp.get('error') self._handle_error()
if not error:
def _handle_error(self):
if not 'error' in self:
return return
code = error['code'] template = 'Error {error[code]}: {error[msg]}'
msg = error['msg'] raise Exception(template.format(**self))
template = 'Error {code}: {msg}'
raise Exception(template.format(code=code, msg=msg))
class Document(dict): class Document(dict):
@ -141,9 +141,6 @@ class Pod(ErrorHandler, Document):
numsubpods=int, numsubpods=int,
subpod=Subpod.from_doc, subpod=Subpod.from_doc,
) )
def __init__(self, *args, **kwargs):
super(Pod, self).__init__(*args, **kwargs)
self._handle_error(self)
@property @property
def subpods(self): def subpods(self):
@ -176,7 +173,6 @@ class Result(ErrorHandler, Document):
def __init__(self, stream): def __init__(self, stream):
doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
super(Result, self).__init__(doc) super(Result, self).__init__(doc)
self._handle_error(self)
@property @property
def info(self): def info(self):