exception_handler.py 729 B

123456789101112131415161718192021222324252627
  1. import traceback
  2. from http import HTTPStatus
  3. from kui.asgi import HTTPException, JSONResponse
  4. class ExceptionHandler:
  5. async def http_exception_handler(self, exc: HTTPException):
  6. return JSONResponse(
  7. dict(
  8. statusCode=exc.status_code,
  9. message=exc.content,
  10. error=HTTPStatus(exc.status_code).phrase,
  11. ),
  12. exc.status_code,
  13. exc.headers,
  14. )
  15. async def other_exception_handler(self, exc: Exception):
  16. traceback.print_exc()
  17. status = HTTPStatus.INTERNAL_SERVER_ERROR
  18. return JSONResponse(
  19. dict(statusCode=status, message=str(exc), error=status.phrase),
  20. status,
  21. )