-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
66 lines (45 loc) · 1.91 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import Optional, Dict, Any
from fastapi import HTTPException
from app import logger
from app.settings import settings as s
UNPROCESSABLE_422 = 422
NOTFOUND_404 = 404
PERMISSIONDENIED_403 = 403
BADERROR_503 = 503
class BaseAppError(HTTPException):
message = 'NO MESSAGE FOUND'
status_code = UNPROCESSABLE_422
def __init__(self, detail: Optional[Any] = None, *, status_code: Optional[int] = None,
headers: Optional[Dict[str, Any]] = None) -> None:
detail = detail or self.message
status_code = status_code or self.status_code
super().__init__(status_code=status_code, detail=detail, headers=headers)
class NotFoundError(BaseAppError):
message = "DATA NOT FOUND"
# Not 404 since pov is from the client (422) not the app (404)
status_code = UNPROCESSABLE_422
def __init__(self, model: str = None):
message = s.DEBUG and model and f'{model} not found' or self.message
super().__init__(message)
class PermissionDenied(BaseAppError):
"""User doesn't have permission to do something."""
message = 'INSUFFICIENT PERMISSIONS'
status_code = PERMISSIONDENIED_403
class FalsyDataError(BaseAppError):
"""Data is falsy such as '', [], None, {}, set(), False, etc.."""
message = "FALSY DATA OR NONE"
status_code = UNPROCESSABLE_422
class UnusableDataError(BaseAppError):
"""Wrong data type"""
message = 'DATA RECIEVED BUT CANNOT BE USED'
status_code = UNPROCESSABLE_422
def __init__(self, model: str = None):
message = s.DEBUG and model and f'{model} not found' or self.message
super().__init__(message)
class ServiceError(BaseAppError):
"""Unable to continue work because of a database error."""
message = 'UNABLE TO PROCESS DATA'
status_code = BADERROR_503
class AppError(BaseAppError):
"""All other errors"""
message = 'UNABLE TO ACCESS APPLICATION AT THIS TIME'