Commit 443c1f01 authored by Vangelis Koukis's avatar Vangelis Koukis
Browse files

Include HTTP status in ClientError exceptions

parent d3a67ae8
......@@ -40,10 +40,22 @@ from urlparse import urlparse
class ClientError(Exception):
def __init__(self, message, details=''):
def __init__(self, message, status=0, details=''):
self.message = message
self.status = status
self.details = details
def __int__(self):
return int(self.status)
def __str__(self):
r = self.message
if self.status:
r += "\nHTTP Status: %d" % self.status
if self.details:
r += "\nDetails: \n%s" % self.details
return r
class Client(object):
def __init__(self, url, token=''):
......@@ -58,7 +70,7 @@ class Client(object):
elif p.scheme == 'https':
conn = HTTPSConnection(p.netloc)
raise ClientError("Unknown URL scheme")
raise ClientError('Unknown URL scheme')
headers = {'X-Auth-Token': self.token}
if body:
......@@ -88,7 +100,7 @@ class Client(object):
reply = json.loads(buf) if buf else {}
except ValueError:
raise ClientError('Invalid response from the server', buf)
raise ClientError('Did not receive valid JSON reply', buf)
if resp.status != success:
if len(reply) == 1:
......@@ -96,7 +108,7 @@ class Client(object):
val = reply[key]
message = '%s: %s' % (key, val.get('message', ''))
details = val.get('details', '')
raise ClientError(message, details)
raise ClientError(message, resp.status, details)
raise ClientError('Invalid response from the server')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment