Skip to content

Commit

Permalink
added the custom exception for the recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
bazarnov committed Jan 28, 2025
1 parent 5f5d921 commit 0599b82
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
_NOOP_MESSAGE_REPOSITORY = NoopMessageRepository()


class ResponseKeysMaxRecurtionReached(AirbyteTracedException):
"""
Raised when the max level of recursion is reached, when trying to
find-and-get the target key, during the `_get_refresh_access_token_response`
"""


class AbstractOauth2Authenticator(AuthBase):
"""
Abstract class for an OAuth authenticators that implements the OAuth token refresh flow. The authenticator
Expand Down Expand Up @@ -157,6 +164,8 @@ def _get_refresh_access_token_response(self) -> Any:
# log the response even if the request failed for troubleshooting purposes
self._log_response(response)
response.raise_for_status()
except ResponseKeysMaxRecurtionReached as e:
raise e
except requests.exceptions.RequestException as e:
if e.response is not None:
if e.response.status_code == 429 or e.response.status_code >= 500:
Expand All @@ -167,8 +176,6 @@ def _get_refresh_access_token_response(self) -> Any:
internal_message=message, message=message, failure_type=FailureType.config_error
)
raise
except AirbyteTracedException as e:
raise e
except Exception as e:
raise Exception(f"Error while refreshing access token: {e}") from e

Expand Down Expand Up @@ -323,7 +330,7 @@ def _find_and_get_value_from_response(
if current_depth > max_depth:
# this is needed to avoid an inf loop, possible with a very deep nesting observed.
message = f"The maximum level of recursion is reached. Couldn't find the speficied `{key_name}` in the response."
raise AirbyteTracedException(
raise ResponseKeysMaxRecurtionReached(
internal_message=message, message=message, failure_type=FailureType.config_error
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
SingleUseRefreshTokenOauth2Authenticator,
TokenAuthenticator,
)
from airbyte_cdk.sources.streams.http.requests_native_auth.abstract_oauth import (
ResponseKeysMaxRecurtionReached,
)
from airbyte_cdk.utils import AirbyteTracedException

LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -342,7 +345,7 @@ def test_refresh_access_token(self, mocker):
}
},
)
with pytest.raises(AirbyteTracedException) as exc_info:
with pytest.raises(ResponseKeysMaxRecurtionReached) as exc_info:
oauth.refresh_access_token()
error_message = "The maximum level of recursion is reached. Couldn't find the speficied `access_token` in the response."
assert exc_info.value.internal_message == error_message
Expand Down

0 comments on commit 0599b82

Please sign in to comment.