fix BundleError handler: map malformed bundles to 422 with structured envelope
- Added exception handler for BundleError in app.py that returns HTTP 422 - Response includes code='invalid_bundle' and message from error argument - Previously returned unstructured 500; now consistent with other validation errors
This commit is contained in:
@@ -34,6 +34,18 @@ from uuid import uuid4
|
||||
|
||||
from fastapi import Depends, FastAPI, Request, Response
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
|
||||
# Import BundleError for exception handling; it's defined in audio_import module.
|
||||
BundleError: Any = None # type: ignore[assignment]
|
||||
try:
|
||||
from .audio_import import BundleError # noqa: F401
|
||||
except ImportError:
|
||||
# BundleError may not exist in older versions or when imported early;
|
||||
# suppress error so the app still starts. The exception handler will be
|
||||
# skipped if BundleError is None.
|
||||
pass
|
||||
|
||||
from typing import Any
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from starlette.datastructures import MutableHeaders
|
||||
@@ -570,6 +582,21 @@ def create_app(
|
||||
},
|
||||
)
|
||||
|
||||
# Conditionally register BundleError handler only if BundleError exists.
|
||||
if BundleError is not None: # type: ignore[comparison-with-none]
|
||||
@app.exception_handler(BundleError)
|
||||
async def _handle_bundle_error(_request: Request, error: BundleError) -> JSONResponse:
|
||||
# Malformed bundles get 422 with structured envelope instead of raw 500.
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content={
|
||||
"error": {
|
||||
"code": "invalid_bundle",
|
||||
"message": str(error),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
from .routes_auth import router as auth_router
|
||||
from .routes_compute import admin_router as compute_router
|
||||
from .routes_compute import agent_router as node_router
|
||||
|
||||
Reference in New Issue
Block a user