aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <f@230.to>2013-09-23 17:35:54 +0300
committerFilipp Lepalaan <f@230.to>2013-09-23 17:35:54 +0300
commit86a6df80b0e40855097fcf836cf11bd4808882f6 (patch)
tree9bc6c3c4766e7ae5a21403c01f317b2a0b34b1c7
parentd78047ab44809537d21fe048d63a4c3c8e96df3d (diff)
downloadpy-gsxws-86a6df80b0e40855097fcf836cf11bd4808882f6.tar.gz
py-gsxws-86a6df80b0e40855097fcf836cf11bd4808882f6.tar.bz2
py-gsxws-86a6df80b0e40855097fcf836cf11bd4808882f6.zip
Added support for multiple error messages
-rw-r--r--gsxws/core.py34
-rw-r--r--tests/fixtures/multierror.xml22
-rw-r--r--tests/test_gsxws.py19
3 files changed, 64 insertions, 11 deletions
diff --git a/gsxws/core.py b/gsxws/core.py
index 9791850..eaf241d 100644
--- a/gsxws/core.py
+++ b/gsxws/core.py
@@ -145,23 +145,39 @@ def get_format(locale=GSX_LOCALE):
class GsxError(Exception):
def __init__(self, message=None, xml=None, url=None):
- self.code = ""
- self.message = message
+ self.codes = []
+ self.messages = []
if xml is not None:
logging.debug(url)
logging.debug(xml)
- el = ET.fromstring(xml)
- self.code = el.findtext("*//faultcode")
- self.message = el.findtext("*//faultstring")
+ root = ET.fromstring(xml)
+
+ for el in root.findall('*//faultcode'):
+ self.codes.append(el.text)
+ for el in root.findall('*//faultstring'):
+ self.messages.append(el.text)
+ for el in root.findall('*//code'):
+ self.codes.append(el.text)
+ for el in root.findall('*//message'):
+ self.messages.append(el.text)
super(Exception, self).__init__(self.message)
- def __unicode__(self):
- return self.message
+ @property
+ def code(self):
+ return self.codes[0]
- def __str__(self):
- return self.message
+ @property
+ def message(self):
+ return unicode(self)
+
+ @property
+ def errors(self):
+ return dict(zip(self.codes, self.messages))
+
+ def __unicode__(self):
+ return u' '.join(self.messages)
class GsxCache(object):
diff --git a/tests/fixtures/multierror.xml b/tests/fixtures/multierror.xml
new file mode 100644
index 0000000..bedb975
--- /dev/null
+++ b/tests/fixtures/multierror.xml
@@ -0,0 +1,22 @@
+<?xml version='1.0' encoding='utf-8'?>
+<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
+ <S:Body>
+ <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
+ <faultcode>GSX.SYS.003</faultcode>
+ <faultstring>Multiple error messages exist. Please check the detail section.</faultstring>
+ <detail>
+ <operationId>iZETtEmzsD82QJs0P1hBgfJ</operationId>
+ <errors>
+ <error>
+ <code>RPR.ONS.025</code>
+ <message>This unit is not eligible for an Onsite repair from GSX.</message>
+ </error>
+ <error>
+ <code>RPR.ONS.026</code>
+ <message>At least one part should be eligible for Onsite.</message>
+ </error>
+ </errors>
+ </detail>
+ </S:Fault>
+ </S:Body>
+</S:Envelope>
diff --git a/tests/test_gsxws.py b/tests/test_gsxws.py
index 6146a2c..59c0662 100644
--- a/tests/test_gsxws.py
+++ b/tests/test_gsxws.py
@@ -8,14 +8,29 @@ from unittest import main, skip, TestCase
from gsxws.objectify import parse
from gsxws.products import Product
-from gsxws import repairs, escalations
+from gsxws import repairs, escalations, GsxError
class RemoteTestCase(TestCase):
def setUp(self):
from gsxws.core import connect
logging.basicConfig(level=logging.DEBUG)
- connect(env['GSX_USER'], env['GSX_PASSWORD'], env['GSX_SOLDTO'], env['GSX_ENV'])
+ connect(env['GSX_USER'],
+ env['GSX_PASSWORD'],
+ env['GSX_SOLDTO'],
+ env['GSX_ENV'])
+
+
+class TestErrorFunctions(TestCase):
+ def setUp(self):
+ xml = open('tests/fixtures/multierror.xml', 'r').read()
+ self.data = GsxError(xml=xml)
+
+ def test_code(self):
+ self.assertEqual(self.data.errors['RPR.ONS.025'],
+ 'This unit is not eligible for an Onsite repair from GSX.')
+ def test_message(self):
+ self.assertRegexpMatches(self.data.message, 'Multiple error messages exist.')
class TestEscalationFunctions(RemoteTestCase):