Skip to content

Commit 1fb1296

Browse files
author
Gauvain Pocentek
committed
Improve error message handling in exceptions
* Depending on the request Gitlab has a 'message' or 'error' attribute in the json data, handle both * Add some consistency by converting messages to unicode or str for exceptions (depending on the python version) Closes #616
1 parent 011274e commit 1fb1296

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

gitlab/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,14 @@ def http_request(self, verb, path, query_data={}, post_data=None,
490490
time.sleep(wait_time)
491491
continue
492492

493+
error_message = result.content
493494
try:
494-
error_message = result.json()['message']
495+
error_json = result.json()
496+
for k in ('message', 'error'):
497+
if k in error_json:
498+
error_message = error_json[k]
495499
except (KeyError, ValueError, TypeError):
496-
error_message = result.content
500+
pass
497501

498502
if result.status_code == 401:
499503
raise GitlabAuthenticationError(

gitlab/exceptions.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ def __init__(self, error_message="", response_code=None,
2828
# Full http response
2929
self.response_body = response_body
3030
# Parsed error message from gitlab
31-
self.error_message = error_message
31+
try:
32+
# if we receive str/bytes we try to convert to unicode/str to have
33+
# consistent message types (see #616)
34+
self.error_message = error_message.decode()
35+
except Exception:
36+
self.error_message = error_message
3237

3338
def __str__(self):
3439
if self.response_code is not None:

0 commit comments

Comments
 (0)