Первый коммит
This commit is contained in:
0
app/core/__init__.py
Normal file
0
app/core/__init__.py
Normal file
BIN
app/core/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
app/core/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/core/__pycache__/constants.cpython-312.pyc
Normal file
BIN
app/core/__pycache__/constants.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/core/__pycache__/error_handlers.cpython-312.pyc
Normal file
BIN
app/core/__pycache__/error_handlers.cpython-312.pyc
Normal file
Binary file not shown.
BIN
app/core/__pycache__/exceptions.cpython-312.pyc
Normal file
BIN
app/core/__pycache__/exceptions.cpython-312.pyc
Normal file
Binary file not shown.
5
app/core/constants.py
Normal file
5
app/core/constants.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from datetime import timedelta
|
||||
|
||||
IDEMPOTENCY_TTL = timedelta(minutes=10)
|
||||
MAX_RETRIES = 5
|
||||
SUPPORTED_SCHEMA_VERSION = "1.0"
|
||||
37
app/core/error_handlers.py
Normal file
37
app/core/error_handlers.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import ValidationError
|
||||
|
||||
from app.core.exceptions import AppError
|
||||
from app.schemas.common import ModuleName
|
||||
|
||||
|
||||
def register_error_handlers(app: FastAPI) -> None:
|
||||
@app.exception_handler(AppError)
|
||||
async def app_error_handler(_: Request, exc: AppError) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"code": exc.code, "desc": exc.desc, "module": exc.module.value},
|
||||
)
|
||||
|
||||
@app.exception_handler(ValidationError)
|
||||
async def validation_error_handler(_: Request, exc: ValidationError) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content={
|
||||
"code": "validation_error",
|
||||
"desc": str(exc),
|
||||
"module": ModuleName.BACKEND.value,
|
||||
},
|
||||
)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def generic_error_handler(_: Request, exc: Exception) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={
|
||||
"code": "internal_error",
|
||||
"desc": str(exc),
|
||||
"module": ModuleName.BACKEND.value,
|
||||
},
|
||||
)
|
||||
9
app/core/exceptions.py
Normal file
9
app/core/exceptions.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from app.schemas.common import ModuleName
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(self, code: str, desc: str, module: ModuleName) -> None:
|
||||
super().__init__(desc)
|
||||
self.code = code
|
||||
self.desc = desc
|
||||
self.module = module
|
||||
Reference in New Issue
Block a user