forked from mirrors/thatmattlove-hyperglass
add logging to error handlers
This commit is contained in:
parent
ec5c0362e3
commit
213da6380b
1 changed files with 17 additions and 1 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
"""API Error Handlers."""
|
"""API Error Handlers."""
|
||||||
|
|
||||||
|
from hyperglass.log import log
|
||||||
|
|
||||||
# Third Party
|
# Third Party
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
from starlette.responses import JSONResponse
|
from starlette.responses import JSONResponse
|
||||||
|
|
@ -11,6 +13,9 @@ from hyperglass.state import use_state
|
||||||
async def default_handler(request: Request, exc: BaseException) -> JSONResponse:
|
async def default_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
"""Handle uncaught errors."""
|
"""Handle uncaught errors."""
|
||||||
state = use_state()
|
state = use_state()
|
||||||
|
log.critical(
|
||||||
|
"{method} {path} {detail!s}", method=request.method, path=request.url.path, detail=exc
|
||||||
|
)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{"output": state.params.messages.general, "level": "danger", "keywords": []},
|
{"output": state.params.messages.general, "level": "danger", "keywords": []},
|
||||||
status_code=500,
|
status_code=500,
|
||||||
|
|
@ -19,7 +24,9 @@ async def default_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
|
|
||||||
async def http_handler(request: Request, exc: BaseException) -> JSONResponse:
|
async def http_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
"""Handle web server errors."""
|
"""Handle web server errors."""
|
||||||
|
log.critical(
|
||||||
|
"{method} {path} {detail}", method=request.method, path=request.url.path, detail=exc.detail
|
||||||
|
)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{"output": exc.detail, "level": "danger", "keywords": []},
|
{"output": exc.detail, "level": "danger", "keywords": []},
|
||||||
status_code=exc.status_code,
|
status_code=exc.status_code,
|
||||||
|
|
@ -28,6 +35,9 @@ async def http_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
|
|
||||||
async def app_handler(request: Request, exc: BaseException) -> JSONResponse:
|
async def app_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
"""Handle application errors."""
|
"""Handle application errors."""
|
||||||
|
log.critical(
|
||||||
|
"{method} {path} {detail}", method=request.method, path=request.url.path, detail=exc.message
|
||||||
|
)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{"output": exc.message, "level": exc.level, "keywords": exc.keywords},
|
{"output": exc.message, "level": exc.level, "keywords": exc.keywords},
|
||||||
status_code=exc.status_code,
|
status_code=exc.status_code,
|
||||||
|
|
@ -37,6 +47,12 @@ async def app_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
async def validation_handler(request: Request, exc: BaseException) -> JSONResponse:
|
async def validation_handler(request: Request, exc: BaseException) -> JSONResponse:
|
||||||
"""Handle Pydantic validation errors raised by FastAPI."""
|
"""Handle Pydantic validation errors raised by FastAPI."""
|
||||||
error = exc.errors()[0]
|
error = exc.errors()[0]
|
||||||
|
log.critical(
|
||||||
|
"{method} {path} {detail}",
|
||||||
|
method=request.method,
|
||||||
|
path=request.url.path,
|
||||||
|
detail=error["msg"],
|
||||||
|
)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
{"output": error["msg"], "level": "error", "keywords": error["loc"]},
|
{"output": error["msg"], "level": "error", "keywords": error["loc"]},
|
||||||
status_code=422,
|
status_code=422,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue