Skip to content

Commit

Permalink
Restore correct exceptions
Browse files Browse the repository at this point in the history
Match the exceptions raised in v3 for v4.

Also update the doc strings with correct information.
  • Loading branch information
Gauvain Pocentek committed Jul 15, 2017
1 parent 374a6c4 commit c15ba3b
Show file tree
Hide file tree
Showing 5 changed files with 655 additions and 148 deletions.
18 changes: 12 additions & 6 deletions gitlab/__init__.py
Expand Up @@ -654,6 +654,10 @@ def http_request(self, verb, path, query_data={}, post_data={},
if 200 <= result.status_code < 300:
return result

if result.status_code == 401:
raise GitlabAuthenticationError(response_code=result.status_code,
error_message=result.content)

raise GitlabHttpError(response_code=result.status_code,
error_message=result.content)

Expand All @@ -674,7 +678,7 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs):
Raises:
GitlabHttpError: When the return code is not 2xx
GitlabParsingError: IF the json data could not be parsed
GitlabParsingError: If the json data could not be parsed
"""
result = self.http_request('get', path, query_data=query_data,
streamed=streamed, **kwargs)
Expand Down Expand Up @@ -706,7 +710,7 @@ def http_list(self, path, query_data={}, **kwargs):
Raises:
GitlabHttpError: When the return code is not 2xx
GitlabParsingError: IF the json data could not be parsed
GitlabParsingError: If the json data could not be parsed
"""
url = self._build_url(path)
get_all = kwargs.pop('all', False)
Expand All @@ -726,19 +730,21 @@ def http_post(self, path, query_data={}, post_data={}, **kwargs):
Returns:
The parsed json returned by the server if json is return, else the
raw content.
raw content
Raises:
GitlabHttpError: When the return code is not 2xx
GitlabParsingError: IF the json data could not be parsed
GitlabParsingError: If the json data could not be parsed
"""
result = self.http_request('post', path, query_data=query_data,
post_data=post_data, **kwargs)
try:
return result.json()
if result.headers.get('Content-Type', None) == 'application/json':
return result.json()
except Exception:
raise GitlabParsingError(
error_message="Failed to parse the server message")
return result

def http_put(self, path, query_data={}, post_data={}, **kwargs):
"""Make a PUT request to the Gitlab server.
Expand All @@ -756,7 +762,7 @@ def http_put(self, path, query_data={}, post_data={}, **kwargs):
Raises:
GitlabHttpError: When the return code is not 2xx
GitlabParsingError: IF the json data could not be parsed
GitlabParsingError: If the json data could not be parsed
"""
result = self.http_request('put', path, query_data=query_data,
post_data=post_data, **kwargs)
Expand Down
20 changes: 20 additions & 0 deletions gitlab/exceptions.py
Expand Up @@ -210,3 +210,23 @@ class to raise. Should be inherited from GitLabError
raise error(error_message=message,
response_code=response.status_code,
response_body=response.content)


def on_http_error(error):
"""Manage GitlabHttpError exceptions.
This decorator function can be used to catch GitlabHttpError exceptions
raise specialized exceptions instead.
Args:
error(Exception): The exception type to raise -- must inherit from
GitlabError
"""
def wrap(f):
def wrapped_f(*args, **kwargs):
try:
return f(*args, **kwargs)
except GitlabHttpError as e:
raise error(e.response_code, e.error_message)
return wrapped_f
return wrap

0 comments on commit c15ba3b

Please sign in to comment.