This repository was archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.py
64 lines (50 loc) · 1.65 KB
/
error.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
from enum import StrEnum
from typing import Optional
from fastapi import Request
from fastapi.responses import JSONResponse
class ErrorKeys(StrEnum):
REGISTER = "invalid_register"
RANKING_UPDATE = "invalid_ranking_update"
DATA = "invalid_data_load"
GET_CLASS = "invalid_get_class"
CHECK = "invalid_code_check"
UPDATE = "invalid_update"
class AppError(Exception):
err_type: ErrorKeys
err_desc: str
debug_key: Optional[str]
def __init__(
self,
err_type: ErrorKeys,
err_desc: str,
debug_key: Optional[str] = None,
):
super().__init__(err_desc)
self.err_type = err_type
self.err_desc = err_desc
self.debug_key = debug_key
class ErrorResponse(Exception):
"""Exception response type that conforms to standard OAuth 2.0 error response in JSON form."""
def __init__(
self,
status_code: int,
err_type: str,
err_desc: str,
debug_key: Optional[str] = None,
):
self.status_code = status_code
self.err_type = err_type
self.err_desc = err_desc
self.debug_key = debug_key
def error_response_return(
err_status_code: int,
err_type: str,
err_desc: str,
err_debug_key: Optional[str] = None,
) -> JSONResponse:
content = {"error": err_type, "error_description": err_desc}
if err_debug_key is not None:
content["debug_key"] = err_debug_key
return JSONResponse(status_code=err_status_code, content=content)
def error_response_handler(_request: Request, e: ErrorResponse) -> JSONResponse:
return error_response_return(e.status_code, e.err_type, e.err_desc, e.debug_key)