diff options
| author | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-11-07 18:17:57 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-11-07 18:51:48 +0900 |
| commit | 09eb674db4a877c4d0acb3040af2e619000fd872 (patch) | |
| tree | 2be5713835eeae97b5613c556fe34ed3ba7f74b5 /pygerrit/rest/__init__.py | |
| parent | a89eb7ab41fcdc3c1502ab763f3ba8c9d491298f (diff) | |
| download | pygerrit-09eb674db4a877c4d0acb3040af2e619000fd872.tar.gz | |
Simplify REST API error handling
Remove GerritRestAPIError. Raise requests.HTTPError when a request
results in an HTTP Error. Raise ValueError in other places.
Change-Id: I25188f64161a6ca8159e7777e3f288f775463c25
Diffstat (limited to 'pygerrit/rest/__init__.py')
| -rw-r--r-- | pygerrit/rest/__init__.py | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/pygerrit/rest/__init__.py b/pygerrit/rest/__init__.py index b5cfd3a..1cfcdc8 100644 --- a/pygerrit/rest/__init__.py +++ b/pygerrit/rest/__init__.py @@ -30,37 +30,17 @@ GERRIT_MAGIC_JSON_PREFIX = ")]}\'\n" GERRIT_AUTH_SUFFIX = "/a" -class GerritRestAPIError(Exception): - - """ Raised when an error occurs during Gerrit REST API access. """ - - def __init__(self, code, message=None): - super(GerritRestAPIError, self).__init__() - self.code = code - self.message = message - - def __str__(self): - if self.message: - return "%d: %s" % (self.code, self.message) - else: - return "%d" % self.code - - def _decode_response(response): """ Decode the `response` received from a REST API call. Strip off Gerrit's magic prefix if it is there, and return decoded JSON content or raw text if it cannot be decoded as JSON. - Raise GerritRestAPIError if the response contains an HTTP error status + Raise requests.HTTPError if the response contains an HTTP error status code. """ - try: - response.raise_for_status() - except requests.exceptions.HTTPError as e: - raise GerritRestAPIError(response.status_code, str(e)) - + response.raise_for_status() content = response.content if content.startswith(GERRIT_MAGIC_JSON_PREFIX): content = content[len(GERRIT_MAGIC_JSON_PREFIX):] @@ -95,8 +75,8 @@ class GerritRestAPI(object): if auth: if not isinstance(auth, requests.auth.AuthBase): - raise GerritRestAPIError('Invalid auth type; must be derived ' - 'from requests.auth.AuthBase') + raise ValueError('Invalid auth type; must be derived ' + 'from requests.auth.AuthBase') if not self.url.endswith(GERRIT_AUTH_SUFFIX): self.url += GERRIT_AUTH_SUFFIX @@ -114,6 +94,8 @@ class GerritRestAPI(object): Strip leading slashes off the endpoint, and return the full url. + Raise requests.RequestException on timeout or connection error. + """ endpoint = endpoint.lstrip('/') return self.url + endpoint @@ -123,6 +105,8 @@ class GerritRestAPI(object): Return JSON decoded result. + Raise requests.RequestException on timeout or connection error. + """ kwargs = self.kwargs.copy() if params: @@ -135,6 +119,8 @@ class GerritRestAPI(object): Return JSON decoded result. + Raise requests.RequestException on timeout or connection error. + """ kwargs = self.kwargs.copy() if params: @@ -149,6 +135,8 @@ class GerritRestAPI(object): Return JSON decoded result. + Raise requests.RequestException on timeout or connection error. + """ kwargs = self.kwargs.copy() if params: @@ -163,6 +151,8 @@ class GerritRestAPI(object): Return JSON decoded result. + Raise requests.RequestException on timeout or connection error. + """ kwargs = self.kwargs.copy() response = self.session.delete(self.make_url(endpoint), **kwargs) |
