From d37d47430eae1bcb9c220ffc850cbe3589545a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Sat, 23 Jun 2018 11:19:36 +0000 Subject: [PATCH] Refactor exceptions --- re2oapi/client.py | 12 ++++++------ re2oapi/endpoints.py | 2 +- re2oapi/exceptions.py | 33 +++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/re2oapi/client.py b/re2oapi/client.py index f3748be..b849003 100644 --- a/re2oapi/client.py +++ b/re2oapi/client.py @@ -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 { diff --git a/re2oapi/endpoints.py b/re2oapi/endpoints.py index ca74c18..212dac2 100644 --- a/re2oapi/endpoints.py +++ b/re2oapi/endpoints.py @@ -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) diff --git a/re2oapi/exceptions.py b/re2oapi/exceptions.py index ac76966..4239557 100644 --- a/re2oapi/exceptions.py +++ b/re2oapi/exceptions.py @@ -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 ({})."