From d81e40dd7df8ef7e45942d60dd84f740fcdfa9c9 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 31 Jan 2021 23:10:25 +0100 Subject: Better error handling --- pyfunkwhale/client.py | 13 +++++++++---- pyfunkwhale/funkwhale.py | 24 ++++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pyfunkwhale/client.py b/pyfunkwhale/client.py index 7343e94..347bcbb 100644 --- a/pyfunkwhale/client.py +++ b/pyfunkwhale/client.py @@ -96,10 +96,15 @@ class Client(object): except FileNotFoundError: raise InvalidTokenError(self) - def _set_token(self, authorization_code): + def _set_token(self, authorization_code: str = None): """ Use the authorization_code to fetch the new token. """ + if ( + getattr(self, 'authorization_code', False) + and not authorization_code + ): + raise InvalidTokenError(self) self.authorization_code = authorization_code self.token = self.oauth_client.fetch_token( @@ -122,15 +127,15 @@ class Client(object): refresh_token=self.token["refresh_token"], client_id=self.client_id, client_secret=self.client_secret) + write_file(self.token_filename, self.token) except InvalidScopeError: - self._get_token() - write_file(self.token_filename, self.token) + raise InvalidTokenError(self) def _force_refresh_token(self): """ Force the refresh of the OAuth2 token """ - self._get_token() + self._set_token() write_file(self.token_filename, self.token) def _get_JWT_token(self) -> dict: diff --git a/pyfunkwhale/funkwhale.py b/pyfunkwhale/funkwhale.py index 47e6b84..0765dc3 100644 --- a/pyfunkwhale/funkwhale.py +++ b/pyfunkwhale/funkwhale.py @@ -94,8 +94,12 @@ class Funkwhale(object): ordering_field = ['creation_date', 'id', 'name'] if ordering is not None and ordering not in ordering_field: - raise ValueError("The ordering field {} is not in the ordering" - "fields accepted".format(ordering)) + raise ValueError( + "The ordering field '{}' is not in the ordering fields " + "accepted. Accepted values: {}".format( + ordering, ordering_field + ) + ) params = self._build_params(arguments) @@ -174,8 +178,12 @@ class Funkwhale(object): ordering_field = ['creation_date', 'release_date', 'title'] if ordering is not None and ordering not in ordering_field: - raise ValueError("The ordering field {} is not in the ordering" - "fields accepted".format(ordering)) + raise ValueError( + "The ordering field '{}' is not in the ordering fields " + "accepted. Accepted values: {}".format( + ordering, ordering_field + ) + ) params = self._build_params(arguments) @@ -259,8 +267,12 @@ class Funkwhale(object): ordering_field = ['creation_date', 'release_date', 'title'] if ordering is not None and ordering not in ordering_field: - raise ValueError("The ordering field {} is not in the ordering" - "fields accepted".format(ordering)) + raise ValueError( + "The ordering field '{}' is not in the ordering fields " + "accepted. Accepted values: {}".format( + ordering, ordering_field + ) + ) params = self._build_params(arguments) -- cgit v1.2.1