Coverage for app/models/errors.py: 86%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-05-02 02:49 +0000

1from app.models.status_codes import StatusCodes 

2 

3 

4 

5class BaseError(Exception): 

6 """Base class for custom errors.""" 

7 def __init__(self, message, status_code=StatusCodes.BAD_REQUEST): 

8 self.message = message 

9 self.status_code = status_code 

10 print("ERROR: " + message) 

11 super().__init__(message) 

12 

13 

14class UserAlreadyExistsError(BaseError): 

15 """Raised when trying to create a user with an existing email.""" 

16 def __init__(self): 

17 super().__init__("Email already exists", StatusCodes.BAD_REQUEST) 

18 

19 

20class UserNotFoundError(BaseError): 

21 """Raised when a user is not found in the database.""" 

22 def __init__(self): 

23 super().__init__("User not found", StatusCodes.NOT_FOUND) 

24 

25 

26class DatabaseError(BaseError): 

27 """Raised for general database errors.""" 

28 def __init__(self, message="A database error occurred"): 

29 super().__init__(message, StatusCodes.SERVER_ERROR) 

30 

31 

32class AuthenticationError(BaseError): 

33 """Raised for authentication failures.""" 

34 def __init__(self, message="Authentication failed"): 

35 super().__init__(message, StatusCodes.UNAUTHORIZED) 

36 

37class ModelError(BaseError): 

38 def __init__(self, message="Suggestion generation failed"): 

39 super().__init__(message, StatusCodes.SERVER_ERROR)