You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
1.1 KiB

  1. from typing import Any, Dict, Optional, Sequence, Type
  2. from pydantic import BaseModel, ValidationError, create_model
  3. from pydantic.error_wrappers import ErrorList
  4. from starlette.exceptions import HTTPException as StarletteHTTPException
  5. class HTTPException(StarletteHTTPException):
  6. def __init__(
  7. self,
  8. status_code: int,
  9. detail: Any = None,
  10. headers: Optional[Dict[str, Any]] = None,
  11. ) -> None:
  12. super().__init__(status_code=status_code, detail=detail)
  13. self.headers = headers
  14. RequestErrorModel: Type[BaseModel] = create_model("Request")
  15. WebSocketErrorModel: Type[BaseModel] = create_model("WebSocket")
  16. class FastAPIError(RuntimeError):
  17. """
  18. A generic, FastAPI-specific error.
  19. """
  20. class RequestValidationError(ValidationError):
  21. def __init__(self, errors: Sequence[ErrorList], *, body: Any = None) -> None:
  22. self.body = body
  23. super().__init__(errors, RequestErrorModel)
  24. class WebSocketRequestValidationError(ValidationError):
  25. def __init__(self, errors: Sequence[ErrorList]) -> None:
  26. super().__init__(errors, WebSocketErrorModel)