Skip to content

Enhancement: Add specific exception types for OAuth2 failures #35

@knowlen

Description

@knowlen

Description

As identified in PR #32 review, the current OAuth2 implementation uses generic exception catching which could be more specific to help developers handle different failure scenarios.

Current State

  • Generic Exception and ValueError types used
  • Limited ability to handle specific OAuth2 error cases programmatically

Proposed Solution

Create specific exception types for common OAuth2 failures:

class OAuth2Error(Exception):
    '''Base exception for OAuth2 operations'''
    pass

class InvalidGrantError(OAuth2Error):
    '''The provided authorization grant is invalid, expired, or revoked'''
    pass

class InvalidClientError(OAuth2Error):
    '''Client authentication failed'''
    pass

class TokenExpiredError(OAuth2Error):
    '''The access token has expired'''
    pass

class InvalidScopeError(OAuth2Error):
    '''The requested scope is invalid, unknown, or malformed'''
    pass

class RateLimitError(OAuth2Error):
    '''OAuth2 endpoint rate limit exceeded'''
    pass

Implementation Notes

  • Map OAuth2 error responses to specific exceptions
  • Maintain backward compatibility by inheriting from base OAuth2Error
  • Include error details from API response in exception

Benefits

  • Better error handling in user applications
  • Clearer debugging experience
  • Follows Python best practices for exception hierarchy

Example Usage

try:
    token = await exchange_authorization_code_async(...)
except InvalidGrantError:
    # Handle expired/invalid authorization code
    pass
except TokenExpiredError:
    # Trigger token refresh
    pass
except OAuth2Error as e:
    # Handle other OAuth2 errors
    logger.error(f'OAuth2 error: {e}')

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions