From 7bee5597409256c83baaa0da64a9c84543e06cbe Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 3 Sep 2016 15:36:52 -0400 Subject: [PATCH] Rely on inheritance to handle errors in Result and Pod. --- wolframalpha/__init__.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/wolframalpha/__init__.py b/wolframalpha/__init__.py index 4057180..61fa166 100644 --- a/wolframalpha/__init__.py +++ b/wolframalpha/__init__.py @@ -56,16 +56,16 @@ class Client(object): class ErrorHandler(object): - @staticmethod - def _handle_error(resp): - error = resp.get('error') - if not error: + def __init__(self, *args, **kwargs): + super(ErrorHandler, self).__init__(*args, **kwargs) + self._handle_error() + + def _handle_error(self): + if not 'error' in self: return - code = error['code'] - msg = error['msg'] - template = 'Error {code}: {msg}' - raise Exception(template.format(code=code, msg=msg)) + template = 'Error {error[code]}: {error[msg]}' + raise Exception(template.format(**self)) class Document(dict): @@ -141,9 +141,6 @@ class Pod(ErrorHandler, Document): numsubpods=int, subpod=Subpod.from_doc, ) - def __init__(self, *args, **kwargs): - super(Pod, self).__init__(*args, **kwargs) - self._handle_error(self) @property def subpods(self): @@ -176,7 +173,6 @@ class Result(ErrorHandler, Document): def __init__(self, stream): doc = xmltodict.parse(stream, dict_constructor=dict)['queryresult'] super(Result, self).__init__(doc) - self._handle_error(self) @property def info(self):