Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
kamaki
Commits
443c1f01
Commit
443c1f01
authored
Sep 28, 2011
by
Vangelis Koukis
Browse files
Include HTTP status in ClientError exceptions
parent
d3a67ae8
Changes
1
Hide whitespace changes
Inline
Side-by-side
kamaki/client.py
View file @
443c1f01
...
...
@@ -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
+=
"
\n
HTTP Status: %d"
%
self
.
status
if
self
.
details
:
r
+=
"
\n
Details:
\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
)
else
:
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):
try
:
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
)
else
:
raise
ClientError
(
'Invalid response from the server'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment