From d098a0f3097988758ef28ab48add4c5e262c8ec0 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Tue, 9 Oct 2018 08:55:52 +0300 Subject: Add custom exception type for connection errors --- gsxws/core.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'gsxws') diff --git a/gsxws/core.py b/gsxws/core.py index 8910927..f9d6eb8 100644 --- a/gsxws/core.py +++ b/gsxws/core.py @@ -213,6 +213,13 @@ class GsxError(Exception): return ' '.join(self.messages) +class GsxConnectionError(GsxError): + """A more fatal-type HTTP error.""" + def __init__(self, url, code, message): + self.messages = [] + self.messages.append('%d: %s (%s)' % (code, message, url)) + + class GsxCache(object): """The cache creates a separate shelf for each GSX session.""" @@ -363,13 +370,16 @@ class GsxRequest(object): else: request.append(self.data) - data = ET.tostring(self.env, "UTF-8") + data = ET.tostring(self.env, 'UTF-8') res = self._send(method, data) - xml = res.text.encode('utf-8') + xml = res.text.encode('utf8') self.xml_response = xml logging.debug("Response: %s %s %s" % (res.status_code, res.reason, xml)) + if res.status_code > 400: + raise GsxConnectionError(self._url, res.status_code, res.reason) + if res.status_code > 200: raise GsxError(xml=xml, url=self._url, status=res.status_code, message=res.reason) @@ -384,7 +394,7 @@ class GsxRequest(object): return ET.tostring(self.env) def __str__(self): - return str(self).encode('utf-8') + return str(self).encode('utf8') class GsxResponse: -- cgit v1.2.3