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 +++++++++++++--- requirements.pip | 2 +- setup.py | 2 +- tests/test_gsxws.py | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) 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: diff --git a/requirements.pip b/requirements.pip index 7bb3fbf..ba55136 100644 --- a/requirements.pip +++ b/requirements.pip @@ -1,3 +1,3 @@ lxml -PyYaml +PyYAML requests diff --git a/setup.py b/setup.py index 98f58e8..3babbb0 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ setup( 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', - 'Topic :: Internet :: WWW/HTTP', + 'Topic :: Software Development :: Libraries :: Python Modules', ], keywords='gsx, python', author='Filipp Lepalaan', diff --git a/tests/test_gsxws.py b/tests/test_gsxws.py index 7dd3412..201a066 100644 --- a/tests/test_gsxws.py +++ b/tests/test_gsxws.py @@ -166,7 +166,7 @@ class CoreFunctionTestCase(TestCase): part = repairs.RepairOrderLine() part.partNumber = '661-5571' rep.orderLines = [part] - self.assertRegex(str(rep.dumps()), r'ääöö') + self.assertRegex(str(rep.dumps()), 'ääöö') def test_cache(self): """Make sure the cache is working.""" -- cgit v1.2.3