1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

38 lines
1.0 KiB
Python
Raw Normal View History

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
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."""
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."""
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."""
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]
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
)