Coverage for src/mcpadapt/auth/exceptions.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-06 19:35 +0530

1"""Custom exceptions for OAuth authentication errors.""" 

2 

3 

4class OAuthError(Exception): 

5 """Base class for all OAuth authentication errors.""" 

6 

7 def __init__(self, message: str, error_code: str | None = None, context: dict | None = None): 

8 """Initialize OAuth error. 

9  

10 Args: 

11 message: Human-readable error message 

12 error_code: Machine-readable error code (optional) 

13 context: Additional context about the error (optional) 

14 """ 

15 super().__init__(message) 

16 self.error_code = error_code 

17 self.context = context or {} 

18 

19 

20class OAuthTimeoutError(OAuthError): 

21 """Raised when OAuth callback doesn't arrive within the specified timeout.""" 

22 

23 def __init__(self, timeout_seconds: int, context: dict | None = None): 

24 message = ( 

25 f"OAuth authentication timed out after {timeout_seconds} seconds. " 

26 f"The user may have closed the browser window or the OAuth server may be unreachable. " 

27 f"Try refreshing the browser or check your network connection." 

28 ) 

29 super().__init__(message, "oauth_timeout", context) 

30 self.timeout_seconds = timeout_seconds 

31 

32 

33class OAuthCancellationError(OAuthError): 

34 """Raised when the user cancels or denies the OAuth authorization.""" 

35 

36 def __init__(self, error_details: str | None = None, context: dict | None = None): 

37 base_message = "OAuth authorization was cancelled or denied by the user." 

38 if error_details: 

39 message = f"{base_message} Details: {error_details}" 

40 else: 

41 message = base_message 

42 super().__init__(message, "oauth_cancelled", context) 

43 self.error_details = error_details 

44 

45 

46class OAuthNetworkError(OAuthError): 

47 """Raised when network-related issues prevent OAuth completion.""" 

48 

49 def __init__(self, original_error: Exception, context: dict | None = None): 

50 message = ( 

51 f"OAuth authentication failed due to network error: {str(original_error)}. " 

52 f"Check your internet connection and try again." 

53 ) 

54 super().__init__(message, "oauth_network_error", context) 

55 self.original_error = original_error 

56 

57 

58class OAuthConfigurationError(OAuthError): 

59 """Raised when OAuth configuration is invalid or incomplete.""" 

60 

61 def __init__(self, config_issue: str, context: dict | None = None): 

62 message = f"OAuth configuration error: {config_issue}" 

63 super().__init__(message, "oauth_config_error", context) 

64 self.config_issue = config_issue 

65 

66 

67class OAuthServerError(OAuthError): 

68 """Raised when the OAuth server returns an error response.""" 

69 

70 def __init__(self, server_error: str, error_description: str | None = None, context: dict | None = None): 

71 message = f"OAuth server error: {server_error}" 

72 if error_description: 

73 message += f" - {error_description}" 

74 super().__init__(message, "oauth_server_error", context) 

75 self.server_error = server_error 

76 self.error_description = error_description 

77 

78 

79class OAuthCallbackError(OAuthError): 

80 """Raised when there's an issue with the OAuth callback handling.""" 

81 

82 def __init__(self, callback_issue: str, context: dict | None = None): 

83 message = f"OAuth callback error: {callback_issue}" 

84 super().__init__(message, "oauth_callback_error", context) 

85 self.callback_issue = callback_issue