2020-01-21 17:27:57 -07:00
|
|
|
"""API Error Handlers."""
|
2020-01-26 02:14:42 -07:00
|
|
|
|
2020-01-21 17:27:57 -07:00
|
|
|
# Third Party Imports
|
|
|
|
from starlette.responses import UJSONResponse
|
|
|
|
|
2020-01-26 02:14:42 -07:00
|
|
|
from hyperglass.configuration import params
|
|
|
|
|
|
|
|
|
|
|
|
async def default_handler(request, exc):
|
|
|
|
"""Handle uncaught errors."""
|
|
|
|
return UJSONResponse(
|
|
|
|
{"output": params.messages.general, "level": "danger", "keywords": []},
|
|
|
|
status_code=500,
|
|
|
|
)
|
|
|
|
|
2020-01-21 17:27:57 -07:00
|
|
|
|
|
|
|
async def http_handler(request, exc):
|
|
|
|
"""Handle web server errors."""
|
|
|
|
return UJSONResponse(
|
|
|
|
{"output": exc.detail, "level": "danger", "keywords": []},
|
|
|
|
status_code=exc.status_code,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def app_handler(request, exc):
|
|
|
|
"""Handle application errors."""
|
|
|
|
return UJSONResponse(
|
|
|
|
{"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 UJSONResponse(
|
|
|
|
{"output": error["msg"], "level": "error", "keywords": error["loc"]},
|
|
|
|
status_code=400,
|
|
|
|
)
|