|
2 | 2 | from fastapi.responses import JSONResponse |
3 | 3 | from fastapi.exceptions import HTTPException |
4 | 4 | from starlette.exceptions import HTTPException as StarletteHTTPException |
| 5 | +from fastapi_users.exceptions import ( |
| 6 | + InvalidID, |
| 7 | + UserAlreadyExists, |
| 8 | + UserNotExists, |
| 9 | + UserInactive, |
| 10 | + UserAlreadyVerified, |
| 11 | + InvalidVerifyToken, |
| 12 | + InvalidResetPasswordToken, |
| 13 | + InvalidPasswordException, |
| 14 | +) |
5 | 15 |
|
6 | 16 | from http import HTTPStatus |
7 | 17 |
|
8 | 18 | from config.setup_logs.logging import logger |
9 | 19 | from api_v1.exeptions import ValidationError |
| 20 | +from api_v1.users.exceptions import PasswordNotValidError, UserNotVerified |
10 | 21 |
|
11 | 22 |
|
12 | 23 | def register_errors(app: FastAPI) -> None: |
@@ -59,6 +70,165 @@ async def file_not_found_error_handler( |
59 | 70 | return JSONResponse(response) |
60 | 71 | ``` |
61 | 72 | """ |
| 73 | + @app.exception_handler(UserNotVerified) |
| 74 | + async def user_not_verify_error_handler( |
| 75 | + request: Request, |
| 76 | + exc: UserNotVerified, |
| 77 | + ): |
| 78 | + """ |
| 79 | + Логирование всех UserNotVerified |
| 80 | + """ |
| 81 | + logger.opt(exception=True).warning(exc) |
| 82 | + response = dict( |
| 83 | + status=False, |
| 84 | + error_code=exc.status_code, |
| 85 | + message=exc.detail, |
| 86 | + ) |
| 87 | + return JSONResponse(response) |
| 88 | + |
| 89 | + @app.exception_handler(InvalidPasswordException) |
| 90 | + async def password_invalid_error_handler( |
| 91 | + request: Request, |
| 92 | + exc: InvalidPasswordException, |
| 93 | + ): |
| 94 | + """ |
| 95 | + Логирование всех InvalidPasswordException |
| 96 | + """ |
| 97 | + logger.opt(exception=True).warning(exc) |
| 98 | + response = dict( |
| 99 | + status=False, |
| 100 | + error_code=exc.status_code, |
| 101 | + message=exc.detail, |
| 102 | + ) |
| 103 | + return JSONResponse(response) |
| 104 | + |
| 105 | + @app.exception_handler(InvalidResetPasswordToken) |
| 106 | + async def password_token_error_handler( |
| 107 | + request: Request, |
| 108 | + exc: InvalidResetPasswordToken, |
| 109 | + ): |
| 110 | + """ |
| 111 | + Логирование всех InvalidResetPasswordToken |
| 112 | + """ |
| 113 | + logger.opt(exception=True).warning(exc) |
| 114 | + response = dict( |
| 115 | + status=False, |
| 116 | + error_code=exc.status_code, |
| 117 | + message=exc.detail, |
| 118 | + ) |
| 119 | + return JSONResponse(response) |
| 120 | + |
| 121 | + @app.exception_handler(InvalidVerifyToken) |
| 122 | + async def verify_token_error_handler( |
| 123 | + request: Request, |
| 124 | + exc: InvalidVerifyToken, |
| 125 | + ): |
| 126 | + """ |
| 127 | + Логирование всех InvalidVerifyToken |
| 128 | + """ |
| 129 | + logger.opt(exception=True).warning(exc) |
| 130 | + response = dict( |
| 131 | + status=False, |
| 132 | + error_code=exc.status_code, |
| 133 | + message=exc.detail, |
| 134 | + ) |
| 135 | + return JSONResponse(response) |
| 136 | + |
| 137 | + @app.exception_handler(UserAlreadyVerified) |
| 138 | + async def user_exists_error_handler( |
| 139 | + request: Request, |
| 140 | + exc: UserAlreadyVerified, |
| 141 | + ): |
| 142 | + """ |
| 143 | + Логирование всех UserAlreadyVerified |
| 144 | + """ |
| 145 | + logger.opt(exception=True).warning(exc) |
| 146 | + response = dict( |
| 147 | + status=False, |
| 148 | + error_code=exc.status_code, |
| 149 | + message=exc.detail, |
| 150 | + ) |
| 151 | + return JSONResponse(response) |
| 152 | + |
| 153 | + @app.exception_handler(UserInactive) |
| 154 | + async def user_activity_error_handler( |
| 155 | + request: Request, |
| 156 | + exc: UserInactive, |
| 157 | + ): |
| 158 | + """ |
| 159 | + Логирование всех UserInactive |
| 160 | + """ |
| 161 | + logger.opt(exception=True).warning(exc) |
| 162 | + response = dict( |
| 163 | + status=False, |
| 164 | + error_code=exc.status_code, |
| 165 | + message=exc.detail, |
| 166 | + ) |
| 167 | + return JSONResponse(response) |
| 168 | + |
| 169 | + @app.exception_handler(UserNotExists) |
| 170 | + async def user_not_exists_error_handler( |
| 171 | + request: Request, |
| 172 | + exc: UserNotExists, |
| 173 | + ): |
| 174 | + """ |
| 175 | + Логирование всех UserNotExists |
| 176 | + """ |
| 177 | + logger.opt(exception=True).warning(exc) |
| 178 | + response = dict( |
| 179 | + status=False, |
| 180 | + error_code=exc.status_code, |
| 181 | + message=exc.detail, |
| 182 | + ) |
| 183 | + return JSONResponse(response) |
| 184 | + |
| 185 | + @app.exception_handler(UserAlreadyExists) |
| 186 | + async def user_already_exists_error_handler( |
| 187 | + request: Request, |
| 188 | + exc: UserAlreadyExists, |
| 189 | + ): |
| 190 | + """ |
| 191 | + Логирование всех UserAlreadyExists |
| 192 | + """ |
| 193 | + logger.opt(exception=True).warning(exc) |
| 194 | + response = dict( |
| 195 | + status=False, |
| 196 | + error_code=exc.status_code, |
| 197 | + message=exc.detail, |
| 198 | + ) |
| 199 | + return JSONResponse(response) |
| 200 | + |
| 201 | + @app.exception_handler(InvalidID) |
| 202 | + async def invalid_id_error_handler( |
| 203 | + request: Request, |
| 204 | + exc: InvalidID, |
| 205 | + ): |
| 206 | + """ |
| 207 | + Логирование всех InvalidID |
| 208 | + """ |
| 209 | + logger.opt(exception=True).warning(exc) |
| 210 | + response = dict( |
| 211 | + status=False, |
| 212 | + error_code=exc.status_code, |
| 213 | + message=exc.detail, |
| 214 | + ) |
| 215 | + return JSONResponse(response) |
| 216 | + |
| 217 | + @app.exception_handler(PasswordNotValidError) |
| 218 | + async def password_validator_error_handler( |
| 219 | + request: Request, |
| 220 | + exc: PasswordNotValidError, |
| 221 | + ): |
| 222 | + """ |
| 223 | + Логирование всех PasswordNotValidError |
| 224 | + """ |
| 225 | + logger.opt(exception=True).warning(exc) |
| 226 | + response = dict( |
| 227 | + status=False, |
| 228 | + error_code=exc.status_code, |
| 229 | + message=exc.detail, |
| 230 | + ) |
| 231 | + return JSONResponse(response) |
62 | 232 |
|
63 | 233 | @app.exception_handler(ValidationError) |
64 | 234 | async def validation_error_handler( |
@@ -109,7 +279,7 @@ async def error_handler( |
109 | 279 | return JSONResponse(response) |
110 | 280 |
|
111 | 281 | @app.exception_handler(StarletteHTTPException) |
112 | | - async def validation_error_handler( |
| 282 | + async def validation_starlette_error_handler( |
113 | 283 | request: Request, |
114 | 284 | exc: StarletteHTTPException, |
115 | 285 | ): |
|
0 commit comments