Skip to content

Commit c15ba3b

Browse files
author
Gauvain Pocentek
committed
Restore correct exceptions
Match the exceptions raised in v3 for v4. Also update the doc strings with correct information.
1 parent 374a6c4 commit c15ba3b

File tree

5 files changed

+655
-148
lines changed

5 files changed

+655
-148
lines changed

gitlab/__init__.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ def http_request(self, verb, path, query_data={}, post_data={},
654654
if 200 <= result.status_code < 300:
655655
return result
656656

657+
if result.status_code == 401:
658+
raise GitlabAuthenticationError(response_code=result.status_code,
659+
error_message=result.content)
660+
657661
raise GitlabHttpError(response_code=result.status_code,
658662
error_message=result.content)
659663

@@ -674,7 +678,7 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs):
674678
675679
Raises:
676680
GitlabHttpError: When the return code is not 2xx
677-
GitlabParsingError: IF the json data could not be parsed
681+
GitlabParsingError: If the json data could not be parsed
678682
"""
679683
result = self.http_request('get', path, query_data=query_data,
680684
streamed=streamed, **kwargs)
@@ -706,7 +710,7 @@ def http_list(self, path, query_data={}, **kwargs):
706710
707711
Raises:
708712
GitlabHttpError: When the return code is not 2xx
709-
GitlabParsingError: IF the json data could not be parsed
713+
GitlabParsingError: If the json data could not be parsed
710714
"""
711715
url = self._build_url(path)
712716
get_all = kwargs.pop('all', False)
@@ -726,19 +730,21 @@ def http_post(self, path, query_data={}, post_data={}, **kwargs):
726730
727731
Returns:
728732
The parsed json returned by the server if json is return, else the
729-
raw content.
733+
raw content
730734
731735
Raises:
732736
GitlabHttpError: When the return code is not 2xx
733-
GitlabParsingError: IF the json data could not be parsed
737+
GitlabParsingError: If the json data could not be parsed
734738
"""
735739
result = self.http_request('post', path, query_data=query_data,
736740
post_data=post_data, **kwargs)
737741
try:
738-
return result.json()
742+
if result.headers.get('Content-Type', None) == 'application/json':
743+
return result.json()
739744
except Exception:
740745
raise GitlabParsingError(
741746
error_message="Failed to parse the server message")
747+
return result
742748

743749
def http_put(self, path, query_data={}, post_data={}, **kwargs):
744750
"""Make a PUT request to the Gitlab server.
@@ -756,7 +762,7 @@ def http_put(self, path, query_data={}, post_data={}, **kwargs):
756762
757763
Raises:
758764
GitlabHttpError: When the return code is not 2xx
759-
GitlabParsingError: IF the json data could not be parsed
765+
GitlabParsingError: If the json data could not be parsed
760766
"""
761767
result = self.http_request('put', path, query_data=query_data,
762768
post_data=post_data, **kwargs)

gitlab/exceptions.py

+20
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,23 @@ class to raise. Should be inherited from GitLabError
210210
raise error(error_message=message,
211211
response_code=response.status_code,
212212
response_body=response.content)
213+
214+
215+
def on_http_error(error):
216+
"""Manage GitlabHttpError exceptions.
217+
218+
This decorator function can be used to catch GitlabHttpError exceptions
219+
raise specialized exceptions instead.
220+
221+
Args:
222+
error(Exception): The exception type to raise -- must inherit from
223+
GitlabError
224+
"""
225+
def wrap(f):
226+
def wrapped_f(*args, **kwargs):
227+
try:
228+
return f(*args, **kwargs)
229+
except GitlabHttpError as e:
230+
raise error(e.response_code, e.error_message)
231+
return wrapped_f
232+
return wrap

0 commit comments

Comments
 (0)