-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexceptions.py
138 lines (98 loc) · 3.32 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from fastapi import Request
from fastapi.responses import JSONResponse
from main import app
class NicknameAlreadyExists(Exception):
pass
class EmailAlreadyExists(Exception):
pass
class UserDoesNotExists(Exception):
pass
class UserIsBanned(Exception):
pass
class PermissionDenied(Exception):
pass
class ArticleDoesNotExists(Exception):
pass
class CommentDoesNotExists(Exception):
pass
class UnexpectedError(Exception):
pass
class UserAccountIsNotVerified(Exception):
pass
class InvalidCategory(Exception):
pass
@app.exception_handler(NicknameAlreadyExists)
async def nickname_already_exists_exception_handler(
request: Request, ex: NicknameAlreadyExists
):
return JSONResponse(
status_code=400,
content={"result": "error", "error_description": "Nickname already exists"},
)
@app.exception_handler(EmailAlreadyExists)
async def nickname_already_exists_exception_handler(
request: Request, ex: NicknameAlreadyExists
):
return JSONResponse(
status_code=400,
content={"result": "error", "error_description": "Email already exists"},
)
@app.exception_handler(UserDoesNotExists)
async def user_does_not_exists_exception_handler(
request: Request, ex: UserDoesNotExists
):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "User does not exist"},
)
@app.exception_handler(UserIsBanned)
async def user_is_banned_exception_handler(request: Request, ex: UserIsBanned):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "User is banned"},
)
@app.exception_handler(PermissionDenied)
async def user_is_banned_exception_handler(request: Request, ex: PermissionDenied):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "Permission denied"},
)
@app.exception_handler(ArticleDoesNotExists)
async def article_does_nor_exists_exception_handler(
request: Request, ex: ArticleDoesNotExists
):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "Article does not exists"},
)
@app.exception_handler(CommentDoesNotExists)
async def comment_does_not_exists_exception_handler(
request: Request, ex: CommentDoesNotExists
):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "Comment does not exists"},
)
@app.exception_handler(UnexpectedError)
async def unexpected_error_exception_handler(request: Request, ex: UnexpectedError):
return JSONResponse(
status_code=500,
content={"result": "error", "error_description": "Unexpected server error"},
)
@app.exception_handler(UserAccountIsNotVerified)
async def user_account_is_not_verified_exception_handler(
request: Request, ex: UserAccountIsNotVerified
):
return JSONResponse(
status_code=401,
content={
"result": "error",
"error_description": "User account is not verified",
},
)
@app.exception_handler(InvalidCategory)
async def invalid_category_exception_handler(request: Request, ex: InvalidCategory):
return JSONResponse(
status_code=405,
content={"result": "error", "error_description": "Category does not exist"},
)