Make sure all HTTP exceptions return JSON with a "message" field instead of "detail"

This commit is contained in:
grossmj committed 2020-11-02 18:08:25 +10:30
1 parent 30d6f1fa2a
commit 5a7b5e4e08
3 files changed
+21 -58

No files matched your search

+11
View File
@@ -17,6 +17,7 @@
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
from gns3server.compute.error import ImageMissingError, NodeError
from gns3server.ubridge.ubridge_error import UbridgeError
@@ -125,6 +126,16 @@ async def ubridge_error_handler(request: Request, exc: UbridgeError):
content={"message": str(exc), "exception": exc.__class__.__name__},
)
# make sure the content key is "message", not "detail" per default
@compute_api.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
compute_api.include_router(capabilities.router, tags=["Capabilities"])
compute_api.include_router(compute.router, tags=["Compute"])
compute_api.include_router(notifications.router, tags=["Notifications"])