generated from admin/template
podex-api completed with swagger & docker compose
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
|
||||
def _timestamp() -> str:
|
||||
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
||||
|
||||
|
||||
def error_body(
|
||||
status_code: int,
|
||||
error_code: str,
|
||||
message: str,
|
||||
path: str,
|
||||
errors: Optional[List[Any]] = None,
|
||||
) -> dict:
|
||||
return {
|
||||
"statusCode": status_code,
|
||||
"errorCode": error_code,
|
||||
"message": message,
|
||||
"errors": errors,
|
||||
"timestamp": _timestamp(),
|
||||
"path": path,
|
||||
}
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(
|
||||
self,
|
||||
status_code: int,
|
||||
error_code: str,
|
||||
message: str,
|
||||
errors: Optional[List[Any]] = None,
|
||||
):
|
||||
self.status_code = status_code
|
||||
self.error_code = error_code
|
||||
self.message = message
|
||||
self.errors = errors
|
||||
|
||||
|
||||
async def app_error_handler(request: Request, exc: AppError) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=error_body(
|
||||
exc.status_code, exc.error_code, exc.message, request.url.path, exc.errors
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def http_exception_handler(
|
||||
request: Request, exc: StarletteHTTPException
|
||||
) -> JSONResponse:
|
||||
code = "INTERNAL_SERVER_ERROR"
|
||||
if exc.status_code == 404:
|
||||
code = "POKEMON_NOT_FOUND"
|
||||
elif exc.status_code == 400:
|
||||
code = "VALIDATION_FAILED"
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=error_body(exc.status_code, code, str(exc.detail), request.url.path),
|
||||
)
|
||||
|
||||
|
||||
async def validation_exception_handler(
|
||||
request: Request, exc: RequestValidationError
|
||||
) -> JSONResponse:
|
||||
errors = []
|
||||
for err in exc.errors():
|
||||
loc = err.get("loc", [])
|
||||
field = ".".join(str(x) for x in loc if x not in ("query", "path", "body"))
|
||||
errors.append(
|
||||
{
|
||||
"field": field,
|
||||
"message": err.get("msg", "invalid value"),
|
||||
"value": err.get("input"),
|
||||
}
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=error_body(
|
||||
400,
|
||||
"VALIDATION_FAILED",
|
||||
"Validation failed for query parameters",
|
||||
request.url.path,
|
||||
errors,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content=error_body(
|
||||
500,
|
||||
"INTERNAL_SERVER_ERROR",
|
||||
"An unexpected error occurred on the server",
|
||||
request.url.path,
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user