__init__.py 698 B

12345678910111213
  1. from fastapi import FastAPI
  2. from fastapi.exceptions import RequestValidationError
  3. from .http_error import httpExceptionHandler
  4. from .validation_error import validationExceptionHandler
  5. from .app_error import appExceptionHandler
  6. from starlette.exceptions import HTTPException as StarletteHTTPException
  7. def registerCustomErrorHandle(server: FastAPI):
  8. """ 统一注册自定义错误处理器"""
  9. # 注册参数验证错误,并覆盖模式RequestValidationError
  10. server.add_exception_handler(RequestValidationError, validationExceptionHandler)
  11. server.add_exception_handler(StarletteHTTPException, httpExceptionHandler)
  12. server.add_exception_handler(Exception, appExceptionHandler)