|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from starlette.middleware.base import BaseHTTPMiddleware |
| 4 | +from starlette.responses import JSONResponse |
| 5 | +from starlette.exceptions import HTTPException |
| 6 | +from starlette.requests import Request |
| 7 | + |
| 8 | +class ResponseMiddleware(BaseHTTPMiddleware): |
| 9 | + def __init__(self, app): |
| 10 | + super().__init__(app) |
| 11 | + |
| 12 | + async def dispatch(self, request, call_next): |
| 13 | + response = await call_next(request) |
| 14 | + |
| 15 | + if isinstance(response, JSONResponse): |
| 16 | + return response |
| 17 | + |
| 18 | + if response.headers.get("content-type") == "application/json": |
| 19 | + try: |
| 20 | + body = b"" |
| 21 | + async for chunk in response.body_iterator: |
| 22 | + body += chunk |
| 23 | + |
| 24 | + raw_data = json.loads(body.decode()) |
| 25 | + |
| 26 | + if isinstance(raw_data, dict) and all(k in raw_data for k in ["code", "data", "msg"]): |
| 27 | + return JSONResponse( |
| 28 | + content=raw_data, |
| 29 | + status_code=response.status_code, |
| 30 | + headers={ |
| 31 | + k: v for k, v in response.headers.items() |
| 32 | + if k.lower() not in ("content-length", "content-type") |
| 33 | + } |
| 34 | + ) |
| 35 | + |
| 36 | + wrapped_data = { |
| 37 | + "code": 0, |
| 38 | + "data": raw_data, |
| 39 | + "msg": None |
| 40 | + } |
| 41 | + |
| 42 | + return JSONResponse( |
| 43 | + content=wrapped_data, |
| 44 | + status_code=response.status_code, |
| 45 | + headers={ |
| 46 | + k: v for k, v in response.headers.items() |
| 47 | + if k.lower() not in ("content-length", "content-type") |
| 48 | + } |
| 49 | + ) |
| 50 | + except Exception as e: |
| 51 | + logging.error(f"Response processing error: {str(e)}", exc_info=True) |
| 52 | + return response |
| 53 | + |
| 54 | + return response |
| 55 | + |
| 56 | + |
| 57 | +class exception_handler(): |
| 58 | + @staticmethod |
| 59 | + async def http_exception_handler(request: Request, exc: HTTPException): |
| 60 | + return JSONResponse( |
| 61 | + status_code=exc.status_code, |
| 62 | + content={ |
| 63 | + "code": exc.status_code, |
| 64 | + "msg": exc.detail, |
| 65 | + "data": None |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + async def global_exception_handler(request: Request, exc: Exception): |
| 72 | + logging.error(f"Error info: {str(exc)}", exc_info=True) |
| 73 | + |
| 74 | + return JSONResponse( |
| 75 | + status_code=500, |
| 76 | + content={ |
| 77 | + "code": 500, |
| 78 | + "msg": str(exc), |
| 79 | + "data": None |
| 80 | + } |
| 81 | + ) |
| 82 | + |
0 commit comments