summaryrefslogtreecommitdiff
path: root/gitlab/exceptions.py
diff options
context:
space:
mode:
authorGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2015-12-31 11:09:36 +0100
committerGauvain Pocentek <gauvain.pocentek@objectif-libre.com>2015-12-31 11:10:56 +0100
commit8fa44554736c4155a1c3b013d29c0625277a2e07 (patch)
treea9c78518dcaeceb5d480e5fe813ea57bcea52891 /gitlab/exceptions.py
parent118b2985249b3b152064af57e03231f1e1c59622 (diff)
downloadgitlab-8fa44554736c4155a1c3b013d29c0625277a2e07.tar.gz
Split code in multiple files
Diffstat (limited to 'gitlab/exceptions.py')
-rw-r--r--gitlab/exceptions.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py
new file mode 100644
index 0000000..f538031
--- /dev/null
+++ b/gitlab/exceptions.py
@@ -0,0 +1,84 @@
+class GitlabError(Exception):
+ def __init__(self, error_message="", response_code=None,
+ response_body=None):
+
+ Exception.__init__(self, error_message)
+ # Http status code
+ self.response_code = response_code
+ # Full http response
+ self.response_body = response_body
+ # Parsed error message from gitlab
+ self.error_message = error_message
+
+ def __str__(self):
+ if self.response_code is not None:
+ return "{0}: {1}".format(self.response_code, self.error_message)
+ else:
+ return "{0}".format(self.error_message)
+
+
+class GitlabAuthenticationError(GitlabError):
+ pass
+
+
+class GitlabConnectionError(GitlabError):
+ pass
+
+
+class GitlabOperationError(GitlabError):
+ pass
+
+
+class GitlabListError(GitlabOperationError):
+ pass
+
+
+class GitlabGetError(GitlabOperationError):
+ pass
+
+
+class GitlabCreateError(GitlabOperationError):
+ pass
+
+
+class GitlabUpdateError(GitlabOperationError):
+ pass
+
+
+class GitlabDeleteError(GitlabOperationError):
+ pass
+
+
+class GitlabProtectError(GitlabOperationError):
+ pass
+
+
+class GitlabTransferProjectError(GitlabOperationError):
+ pass
+
+
+def raise_error_from_response(response, error, expected_code=200):
+ """Tries to parse gitlab error message from response and raises error.
+
+ Do nothing if the response status is the expected one.
+
+ If response status code is 401, raises instead GitlabAuthenticationError.
+
+ response: requests response object
+ error: Error-class to raise. Should be inherited from GitLabError
+ """
+
+ if expected_code == response.status_code:
+ return
+
+ try:
+ message = response.json()['message']
+ except (KeyError, ValueError):
+ message = response.content
+
+ if response.status_code == 401:
+ error = GitlabAuthenticationError
+
+ raise error(error_message=message,
+ response_code=response.status_code,
+ response_body=response.content)