Refactor exceptions
This commit is contained in:
parent
be662c5825
commit
d37d47430e
3 changed files with 30 additions and 17 deletions
|
@ -43,7 +43,7 @@ class Re2oAPIClient:
|
|||
self._password = password
|
||||
try:
|
||||
self.token = self._get_token_from_file()
|
||||
except Exception:
|
||||
except exceptions.APIClientGenericError:
|
||||
self.token = self._get_token_from_server()
|
||||
self._save_token_to_file()
|
||||
|
||||
|
@ -59,7 +59,7 @@ class Re2oAPIClient:
|
|||
|
||||
def _get_token_from_file(self):
|
||||
if not self.token_file.is_file():
|
||||
raise exceptions.TokenFileNotFound(str(self.token_file))
|
||||
raise exceptions.TokenFileNotFound(self.token_file)
|
||||
|
||||
with self.token_file.open() as f:
|
||||
data = json.load(f)
|
||||
|
@ -72,9 +72,9 @@ class Re2oAPIClient:
|
|||
}
|
||||
except KeyError:
|
||||
raise exceptions.TokenNotInTokenFile(
|
||||
"{user}@{host} in {token_file}".format(user=self._username,
|
||||
hostname=self.hostname,
|
||||
token_file=str(self.token_file))
|
||||
self._username,
|
||||
self.hostname,
|
||||
self.token_file
|
||||
)
|
||||
|
||||
def _save_token_to_file(self):
|
||||
|
@ -104,7 +104,7 @@ class Re2oAPIClient:
|
|||
data={'username': self._username, 'password': self._password}
|
||||
)
|
||||
if response.status_code == requests.codes.bad_request:
|
||||
raise exceptions.InvalidCredentials()
|
||||
raise exceptions.InvalidCredentials(self._username, self.hostname)
|
||||
response.raise_for_status()
|
||||
response = response.json()
|
||||
return {
|
||||
|
|
|
@ -117,7 +117,7 @@ def get_endpoint_for(name, **kwargs):
|
|||
try:
|
||||
url=urls[name]
|
||||
except KeyError as e:
|
||||
raise exceptions.URLNameNotExists(name)
|
||||
raise exceptions.URLNameDoesNotExists(name)
|
||||
else:
|
||||
try:
|
||||
return url.format_map(kwargs)
|
||||
|
|
|
@ -1,14 +1,27 @@
|
|||
class URLNameNotExists(ValueError):
|
||||
pass
|
||||
class APIClientGenericError(ValueError):
|
||||
template = "{}"
|
||||
|
||||
class URLParameterMissing(ValueError):
|
||||
pass
|
||||
def __init__(self, *data):
|
||||
self.data = data
|
||||
self.message = self.template.format(*data)
|
||||
super(APIClientGenericError, self).__init__(self.message)
|
||||
|
||||
class InvalidCredentials(ValueError):
|
||||
pass
|
||||
|
||||
class TokenFileNotFound(ValueError):
|
||||
pass
|
||||
class URLNameDoesNotExists(APIClientGenericError):
|
||||
template = "The name '{}' was not foud among the possible endpoints."
|
||||
|
||||
class TokenNotInTokenFile(ValueError):
|
||||
pass
|
||||
|
||||
class URLParameterMissing(APIClientGenericError):
|
||||
template = "The url named '{}' require the parameter '{}' to be built."
|
||||
|
||||
|
||||
class InvalidCredentials(APIClientGenericError):
|
||||
template = "The credentials for {}@{} are not valid."
|
||||
|
||||
|
||||
class TokenFileNotFound(APIClientGenericError):
|
||||
template = "Token file at {} not found."
|
||||
|
||||
|
||||
class TokenNotInTokenFile(APIClientGenericError):
|
||||
template = "Token for {}@{} not found in token file ({})."
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue