2020-01-21 17:27:57 -07:00
|
|
|
"""API Error Handlers."""
|
2020-01-26 02:14:42 -07:00
|
|
|
|
2020-02-03 02:34:50 -07:00
|
|
|
# Third Party
|
2020-04-12 02:10:09 -07:00
|
|
|
from starlette.responses import JSONResponse
|
2020-01-21 17:27:57 -07:00
|
|
|
|
2020-02-03 02:34:50 -07:00
|
|
|
# Project
|
2020-01-26 02:14:42 -07:00
|
|
|
from hyperglass.configuration import params
|
|
|
|
|
|
|
|
|
|
|
|
async def default_handler(request, exc):
|
|
|
|
"""Handle uncaught errors."""
|
2020-04-12 02:10:09 -07:00
|
|
|
return JSONResponse(
|
2021-09-12 15:09:24 -07:00
|
|
|
{"output": params.messages.general, "level": "danger", "keywords": []}, status_code=500,
|
2020-01-26 02:14:42 -07:00
|
|
|
)
|
|
|
|
|
2020-01-21 17:27:57 -07:00
|
|
|
|
|
|
|
async def http_handler(request, exc):
|
|
|
|
"""Handle web server errors."""
|
2020-04-12 02:10:09 -07:00
|
|
|
return JSONResponse(
|
2021-09-12 15:09:24 -07:00
|
|
|
{"output": exc.detail, "level": "danger", "keywords": []}, status_code=exc.status_code,
|
2020-01-21 17:27:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def app_handler(request, exc):
|
|
|
|
"""Handle application errors."""
|
2020-04-12 02:10:09 -07:00
|
|
|
return JSONResponse(
|
2020-01-21 17:27:57 -07:00
|
|
|
{"output": exc.message, "level": exc.level, "keywords": exc.keywords},
|
|
|
|
status_code=exc.status_code,
|
|
|
|
)
|
2020-01-26 02:14:42 -07:00
|
|
|
|
|
|
|
|
|
|
|
async def validation_handler(request, exc):
|
|
|
|
"""Handle Pydantic validation errors raised by FastAPI."""
|
|
|
|
error = exc.errors()[0]
|
2020-04-12 02:10:09 -07:00
|
|
|
return JSONResponse(
|
2021-09-12 15:09:24 -07:00
|
|
|
{"output": error["msg"], "level": "error", "keywords": error["loc"]}, status_code=422,
|
2020-01-26 02:14:42 -07:00
|
|
|
)
|