diff --git a/docs/en/docs/tutorial/handling-errors.md b/docs/en/docs/tutorial/handling-errors.md index 98ac55d1f7722..175820be8e0ab 100644 --- a/docs/en/docs/tutorial/handling-errors.md +++ b/docs/en/docs/tutorial/handling-errors.md @@ -163,7 +163,7 @@ path -> item_id !!! warning These are technical details that you might skip if it's not important for you now. -`RequestValidationError` is a sub-class of Pydantic's `ValidationError`. +`RequestValidationError` is morally a sub-class of Pydantic's `ValidationError`. **FastAPI** uses it so that, if you use a Pydantic model in `response_model`, and your data has an error, you will see the error in your log. diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index 44d4ada86d7e4..e10c8f256046c 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -1,6 +1,7 @@ from typing import Any, Dict, Optional, Sequence, Type, Union from pydantic import BaseModel, create_model +from pydantic_core import ErrorDetails from starlette.exceptions import HTTPException as StarletteHTTPException from starlette.exceptions import WebSocketException as StarletteWebSocketException from typing_extensions import Annotated, Doc @@ -147,15 +148,15 @@ class FastAPIError(RuntimeError): class ValidationException(Exception): - def __init__(self, errors: Sequence[Any]) -> None: + def __init__(self, errors: Sequence[ErrorDetails]) -> None: self._errors = errors - def errors(self) -> Sequence[Any]: + def errors(self) -> Sequence[ErrorDetails]: return self._errors class RequestValidationError(ValidationException): - def __init__(self, errors: Sequence[Any], *, body: Any = None) -> None: + def __init__(self, errors: Sequence[ErrorDetails], *, body: Any = None) -> None: super().__init__(errors) self.body = body @@ -165,7 +166,7 @@ class WebSocketRequestValidationError(ValidationException): class ResponseValidationError(ValidationException): - def __init__(self, errors: Sequence[Any], *, body: Any = None) -> None: + def __init__(self, errors: Sequence[ErrorDetails], *, body: Any = None) -> None: super().__init__(errors) self.body = body