Combine error handling

This commit is contained in:
Jason R. Coombs 2016-09-03 11:49:01 -04:00
parent 760a6a6a92
commit 35cc9e035d

View File

@ -54,13 +54,27 @@ class Client(object):
return Result(resp) return Result(resp)
class Result(object): class ErrorHandler(object):
@staticmethod
def _handle_error(resp):
error_state = resp['@error']
if error_state == 'false':
return
error = resp['error']
code = error['code']
msg = error['msg']
template = 'Error {code}: {msg}'
raise Exception(template.format(code=code, msg=msg))
class Result(ErrorHandler, object):
""" """
Handles processing the response for the programmer. Handles processing the response for the programmer.
""" """
def __init__(self, stream): def __init__(self, stream):
self.tree = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] self.tree = xmltodict.parse(stream, dict_constructor=dict)['queryresult']
self._handle_error() self._handle_error(self.tree)
self.info = [] self.info = []
try: try:
self.pods = list(map(Pod, self.tree['pod'])) self.pods = list(map(Pod, self.tree['pod']))
@ -78,17 +92,6 @@ class Result(object):
except KeyError: except KeyError:
self.warnings = None self.warnings = None
def _handle_error(self):
error_state = self.tree['@error']
if error_state == 'false':
return
error = self.tree['error']
code = error['code']
msg = error['msg']
template = 'Error {code}: {msg}'
raise Exception(template.format(code=code, msg=msg))
def __iter__(self): def __iter__(self):
return iter(self.info) return iter(self.info)
@ -110,14 +113,14 @@ class Result(object):
return {pod.title: pod.text for pod in self.pods} return {pod.title: pod.text for pod in self.pods}
class Pod(object): class Pod(ErrorHandler, object):
""" """
Groups answers and information contextualizing those answers. Groups answers and information contextualizing those answers.
""" """
def __init__(self, node): def __init__(self, node):
self.node = node self.node = node
self.error = node['@error'] self.error = node['@error']
self._handle_error() self._handle_error(self.node)
self.title = node['@title'] self.title = node['@title']
self.scanner = node['@scanner'] self.scanner = node['@scanner']
self.id = node['@id'] self.id = node['@id']
@ -131,16 +134,6 @@ class Pod(object):
self.subpods = list(map(Subpod, self.subpods)) self.subpods = list(map(Subpod, self.subpods))
self.primary = '@primary' in node and node['@primary'] != 'false' self.primary = '@primary' in node and node['@primary'] != 'false'
def _handle_error(self):
if self.error == 'false':
return
error = self.node['error']
code = error['code']
msg = error['msg']
template = 'Error {code}: {msg}'
raise Exception(template.format(code=code, msg=msg))
def __iter__(self): def __iter__(self):
return iter(self.subpods) return iter(self.subpods)