From e9fae14bf8060c3371f2f98dc4e5347869987141 Mon Sep 17 00:00:00 2001 From: Thomas Beyer Date: Tue, 19 Dec 2023 15:13:01 -0800 Subject: [PATCH] trying to handle and refresh expired access token from vague 'Unauthorized' API response (#54) --- capella_console_client/exceptions.py | 5 +++++ capella_console_client/session.py | 1 + 2 files changed, 6 insertions(+) diff --git a/capella_console_client/exceptions.py b/capella_console_client/exceptions.py index 1fa371e..14922d2 100644 --- a/capella_console_client/exceptions.py +++ b/capella_console_client/exceptions.py @@ -67,6 +67,8 @@ class NoRefreshTokenError(CapellaConsoleClientError): NOT_AUTHORIZED_ERROR_CODE = "NOT_AUTHORIZED" ORDER_VALIDATION_ERROR_CODE = "ORDER_VALIDATION_ERROR" +UNAUTHORIZED_MESSAGE = "unauthorized" + ERROR_CODES = { INVALID_TOKEN_ERROR_CODE: AuthenticationError, ORDER_EXPIRED_ERROR_CODE: OrderExpiredError, @@ -107,6 +109,9 @@ def handle_error_response_and_raise(response): code = DEFAULT_ERROR_CODE data = {} + if message is not None and message.lower() == UNAUTHORIZED_MESSAGE and code == DEFAULT_ERROR_CODE: + code = INVALID_TOKEN_ERROR_CODE + # try to assign some more meaningful exception class by message if code == DEFAULT_ERROR_CODE and message: try: diff --git a/capella_console_client/session.py b/capella_console_client/session.py index 40fd293..c47734f 100644 --- a/capella_console_client/session.py +++ b/capella_console_client/session.py @@ -154,6 +154,7 @@ def send(self, *fct_args, **kwargs): # retry request orig_request = fct_args[0] orig_request.headers["authorization"] = self.headers["authorization"] + logger.info(f"retrying {fct_args[0]}") ret = super().send(*fct_args, **kwargs) return ret