1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-17 08:48:05 +00:00
thatmattlove-hyperglass/hyperglass/api/error_handlers.py
2021-12-06 17:12:30 -07:00

42 lines
1.1 KiB
Python

"""API Error Handlers."""
# Third Party
from starlette.responses import JSONResponse
# Project
from hyperglass.state import use_state
async def default_handler(request, exc):
"""Handle uncaught errors."""
state = use_state()
return JSONResponse(
{"output": state.params.messages.general, "level": "danger", "keywords": []},
status_code=500,
)
async def http_handler(request, exc):
"""Handle web server errors."""
return JSONResponse(
{"output": exc.detail, "level": "danger", "keywords": []},
status_code=exc.status_code,
)
async def app_handler(request, exc):
"""Handle application errors."""
return JSONResponse(
{"output": exc.message, "level": exc.level, "keywords": exc.keywords},
status_code=exc.status_code,
)
async def validation_handler(request, exc):
"""Handle Pydantic validation errors raised by FastAPI."""
error = exc.errors()[0]
return JSONResponse(
{"output": error["msg"], "level": "error", "keywords": error["loc"]},
status_code=422,
)