|
4 | 4 | from fastapi.encoders import jsonable_encoder |
5 | 5 | from fastapi.middleware.cors import CORSMiddleware |
6 | 6 | from starlette.responses import JSONResponse |
| 7 | +from starlette.exceptions import HTTPException as StarletteHTTPException |
7 | 8 | from starlette.middleware.base import BaseHTTPMiddleware |
8 | 9 | from app.routes.compile import compile_endpoint |
9 | 10 | from app.process_monitor import process_monitor |
@@ -54,11 +55,29 @@ async def dispatch(self, request: Request, call_next): |
54 | 55 | ) |
55 | 56 |
|
56 | 57 |
|
| 58 | +# This service is a Hasura action webhook. Hasura requires error responses to be |
| 59 | +# shaped as {"message": ...} (parsed into ActionWebhookErrorResponse); FastAPI's |
| 60 | +# default {"detail": ...} makes Hasura fail with |
| 61 | +# 'ActionWebhookErrorResponse ... key "message" not found' and the bot/UI never |
| 62 | +# gets a clean failure (e.g. a normal "compilation failed"). Reshape both |
| 63 | +# HTTPException and request-validation errors into Hasura's format. |
| 64 | +@app.exception_handler(StarletteHTTPException) |
| 65 | +async def http_exception_handler(request: Request, exc: StarletteHTTPException): |
| 66 | + message = exc.detail if isinstance(exc.detail, str) else str(exc.detail) |
| 67 | + return JSONResponse( |
| 68 | + status_code=exc.status_code, |
| 69 | + content={"message": message}, |
| 70 | + ) |
| 71 | + |
| 72 | + |
57 | 73 | @app.exception_handler(RequestValidationError) |
58 | 74 | async def validation_exception_handler(request: Request, exc: RequestValidationError): |
59 | 75 | return JSONResponse( |
60 | | - status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
61 | | - content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}), |
| 76 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 77 | + content={ |
| 78 | + "message": "Invalid compile request.", |
| 79 | + "extensions": {"errors": jsonable_encoder(exc.errors())}, |
| 80 | + }, |
62 | 81 | ) |
63 | 82 |
|
64 | 83 |
|
|
0 commit comments