Skip to content

Commit b761342

Browse files
committed
Attempt to fix error handling
1 parent 96fa466 commit b761342

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

apps/zxbasic/app/main.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from fastapi.encoders import jsonable_encoder
55
from fastapi.middleware.cors import CORSMiddleware
66
from starlette.responses import JSONResponse
7+
from starlette.exceptions import HTTPException as StarletteHTTPException
78
from starlette.middleware.base import BaseHTTPMiddleware
89
from app.routes.compile import compile_endpoint
910
from app.process_monitor import process_monitor
@@ -54,11 +55,29 @@ async def dispatch(self, request: Request, call_next):
5455
)
5556

5657

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+
5773
@app.exception_handler(RequestValidationError)
5874
async def validation_exception_handler(request: Request, exc: RequestValidationError):
5975
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+
},
6281
)
6382

6483

0 commit comments

Comments
 (0)