Coverage for src / mysingle / auth / exceptions.py: 0%

60 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-02 00:58 +0900

1"""Auth 관련 사용자 정의 예외 클래스들""" 

2 

3from typing import Any 

4 

5 

6class AuthException(Exception): 

7 """Auth 시스템의 기본 예외 클래스""" 

8 

9 def __init__( 

10 self, 

11 message: str = "Authentication error occurred", 

12 details: Any | None = None, 

13 ): 

14 self.message = message 

15 self.details = details 

16 super().__init__(self.message) 

17 

18 

19class InvalidID(AuthException): 

20 """유효하지 않은 ID 예외""" 

21 

22 def __init__(self, id_value: Any | None = None): 

23 message = f"Invalid ID format: {id_value}" if id_value else "Invalid ID format" 

24 super().__init__(message, {"id": id_value}) 

25 

26 

27class UserAlreadyExists(AuthException): 

28 """사용자가 이미 존재하는 예외""" 

29 

30 def __init__(self, email: str | None = None): 

31 message = ( 

32 f"User with email '{email}' already exists" 

33 if email 

34 else "User already exists" 

35 ) 

36 super().__init__(message, {"email": email}) 

37 

38 

39class UserNotExists(AuthException): 

40 """사용자가 존재하지 않는 예외""" 

41 

42 def __init__(self, identifier: str | None = None, identifier_type: str = "user"): 

43 message = ( 

44 f"{identifier_type.title()} '{identifier}' not found" 

45 if identifier 

46 else f"{identifier_type.title()} not found" 

47 ) 

48 super().__init__(message, {"identifier": identifier, "type": identifier_type}) 

49 

50 

51class UserInactive(AuthException): 

52 """비활성 사용자 예외""" 

53 

54 def __init__(self, user_id: Any | None = None): 

55 message = f"User '{user_id}' is inactive" if user_id else "User is inactive" 

56 super().__init__(message, {"user_id": user_id}) 

57 

58 

59class UserAlreadyVerified(AuthException): 

60 """이미 인증된 사용자 예외""" 

61 

62 def __init__(self, user_id: Any | None = None): 

63 message = ( 

64 f"User '{user_id}' is already verified" 

65 if user_id 

66 else "User is already verified" 

67 ) 

68 super().__init__(message, {"user_id": user_id}) 

69 

70 

71class InvalidVerifyToken(AuthException): 

72 """유효하지 않은 인증 토큰 예외""" 

73 

74 def __init__(self, reason: str | None = None): 

75 message = ( 

76 f"Invalid verification token: {reason}" 

77 if reason 

78 else "Invalid verification token" 

79 ) 

80 super().__init__(message, {"reason": reason}) 

81 

82 

83class InvalidResetPasswordToken(AuthException): 

84 """유효하지 않은 비밀번호 재설정 토큰 예외""" 

85 

86 def __init__(self, reason: str | None = None): 

87 message = ( 

88 f"Invalid reset password token: {reason}" 

89 if reason 

90 else "Invalid reset password token" 

91 ) 

92 super().__init__(message, {"reason": reason}) 

93 

94 

95class InvalidPasswordException(AuthException): 

96 """유효하지 않은 비밀번호 예외""" 

97 

98 def __init__(self, reason: Any | None = None): 

99 self.reason = reason 

100 message = f"Invalid password: {reason}" if reason else "Invalid password" 

101 super().__init__(message, {"reason": reason}) 

102 

103 

104class AuthenticationFailed(AuthException): 

105 """인증 실패 예외""" 

106 

107 def __init__(self, reason: str = "Invalid credentials"): 

108 super().__init__(f"Authentication failed: {reason}", {"reason": reason}) 

109 

110 

111class AuthorizationFailed(AuthException): 

112 """인가 실패 예외""" 

113 

114 def __init__( 

115 self, required_permission: str | None = None, user_id: Any | None = None 

116 ): 

117 message = ( 

118 f"Authorization failed. Required: {required_permission}" 

119 if required_permission 

120 else "Authorization failed" 

121 ) 

122 super().__init__( 

123 message, {"required_permission": required_permission, "user_id": user_id} 

124 ) 

125 

126 

127class TokenExpired(AuthException): 

128 """토큰 만료 예외""" 

129 

130 def __init__(self, token_type: str = "token"): 

131 message = f"{token_type.title()} has expired" 

132 super().__init__(message, {"token_type": token_type}) 

133 

134 

135class InvalidToken(AuthException): 

136 """유효하지 않은 토큰 예외""" 

137 

138 def __init__(self, token_type: str = "token", reason: str | None = None): 

139 message = ( 

140 f"Invalid {token_type}: {reason}" if reason else f"Invalid {token_type}" 

141 ) 

142 super().__init__(message, {"token_type": token_type, "reason": reason}) 

143 

144 

145class JWTStrategyDestroyNotSupportedError(AuthException): 

146 """JWT 전략에서 토큰 파기가 지원되지 않는 예외""" 

147 

148 def __init__(self): 

149 super().__init__("JWT tokens cannot be destroyed") 

150 

151 

152class OAuth2Error(AuthException): 

153 """OAuth2 관련 예외""" 

154 

155 def __init__( 

156 self, message: str = "OAuth2 error occurred", details: Any | None = None 

157 ): 

158 super().__init__(message, details)