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 | |
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
-rw-r--r-- | pygerrit/rest/__init__.py | 38 | ||||
-rw-r--r-- | pygerrit/rest/auth.py | 8 |
2 files changed, 16 insertions, 30 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) diff --git a/pygerrit/rest/auth.py b/pygerrit/rest/auth.py index 01147af..c43c3fa 100644 --- a/pygerrit/rest/auth.py +++ b/pygerrit/rest/auth.py @@ -25,8 +25,6 @@ from requests.auth import HTTPDigestAuth, HTTPBasicAuth from requests.utils import get_netrc_auth -from . import GerritRestAPIError - class HTTPDigestAuthFromNetrc(HTTPDigestAuth): @@ -35,8 +33,7 @@ class HTTPDigestAuthFromNetrc(HTTPDigestAuth): def __init__(self, url): auth = get_netrc_auth(url) if not auth: - raise GerritRestAPIError("netrc missing or no credentials found " - "in netrc") + raise ValueError("netrc missing or no credentials found in netrc") username, password = auth super(HTTPDigestAuthFromNetrc, self).__init__(username, password) @@ -51,8 +48,7 @@ class HTTPBasicAuthFromNetrc(HTTPBasicAuth): def __init__(self, url): auth = get_netrc_auth(url) if not auth: - raise GerritRestAPIError("netrc missing or no credentials found " - "in netrc") + raise ValueError("netrc missing or no credentials found in netrc") username, password = auth super(HTTPBasicAuthFromNetrc, self).__init__(username, password) |