2020-08-03 16:59:07 +04:30
|
|
|
from typing import Any, Dict, Optional, Sequence
|
2019-05-30 19:43:02 +04:00
|
|
|
|
2019-11-27 11:32:02 -08:00
|
|
|
from pydantic import ValidationError, create_model
|
2019-08-30 17:30:03 -07:00
|
|
|
from pydantic.error_wrappers import ErrorList
|
2019-02-16 17:01:29 +04:00
|
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPException(StarletteHTTPException):
|
|
|
|
|
def __init__(
|
2020-08-03 16:59:07 +04:30
|
|
|
self,
|
|
|
|
|
status_code: int,
|
|
|
|
|
detail: Any = None,
|
|
|
|
|
headers: Optional[Dict[str, Any]] = None,
|
2019-02-16 17:01:29 +04:00
|
|
|
) -> None:
|
|
|
|
|
super().__init__(status_code=status_code, detail=detail)
|
|
|
|
|
self.headers = headers
|
2019-05-29 16:27:55 +04:00
|
|
|
|
|
|
|
|
|
2019-11-27 11:32:02 -08:00
|
|
|
RequestErrorModel = create_model("Request")
|
|
|
|
|
WebSocketErrorModel = create_model("WebSocket")
|
|
|
|
|
|
|
|
|
|
|
2020-02-29 05:04:35 -08:00
|
|
|
class FastAPIError(RuntimeError):
|
|
|
|
|
"""
|
|
|
|
|
A generic, FastAPI-specific error.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2019-05-29 16:27:55 +04:00
|
|
|
class RequestValidationError(ValidationError):
|
2020-01-17 13:37:44 +02:00
|
|
|
def __init__(self, errors: Sequence[ErrorList], *, body: Any = None) -> None:
|
|
|
|
|
self.body = body
|
2020-08-09 22:17:08 +02:00
|
|
|
super().__init__(errors, RequestErrorModel)
|
2019-05-29 16:27:55 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketRequestValidationError(ValidationError):
|
2019-08-30 17:30:03 -07:00
|
|
|
def __init__(self, errors: Sequence[ErrorList]) -> None:
|
2020-08-09 22:17:08 +02:00
|
|
|
super().__init__(errors, WebSocketErrorModel)
|