Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestValidationError is **not** a sub-class of Pydantic's ValidationError #10424

Open
9 tasks done
Kludex opened this issue Oct 11, 2023 Discussed in #10421 · 5 comments · May be fixed by #11542
Open
9 tasks done

RequestValidationError is **not** a sub-class of Pydantic's ValidationError #10424

Kludex opened this issue Oct 11, 2023 Discussed in #10421 · 5 comments · May be fixed by #11542
Labels
docs Documentation about how to use FastAPI question Question or problem

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Oct 11, 2023

Discussed in #10421

Originally posted by valentinoli October 10, 2023

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@app.exception_handler(ValidationError)
async def validation_exception_handler(request: Request, exc: ValidationError):
    return PlainTextResponse(
        str(exc), status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
    )

Description

I was trying to handle both RequestValidationError and ValidationError by writing a custom handler for ValidationError but that doesn't work.

The docs state that

RequestValidationError is a sub-class of Pydantic's ValidationError

However, looking at FastAPI source code reveals that this is not the case.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.101.0

Pydantic Version

2.1.1

Python Version

3.11.3

Additional Context

No response

@Kludex Kludex added question Question or problem docs Documentation about how to use FastAPI labels Oct 11, 2023
@iudeen
Copy link
Contributor

iudeen commented Oct 11, 2023

Should we fix the docs or the code? IMO having Pydantic's error makes more sense.

@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Oct 11, 2023

Documentation. But I'm just bringing it to @tiangolo 's attention.

@FaizanMunsaf
Copy link

hey Please choose this format

Here is main.py

from app.infrastructure.exception import validation_exception_handler

app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_STR}openapi.json"
)

@app.exception_handler(RequestValidationError)
async def custom_validation_exception_handler(request, exc):
    return await validation_exception_handler(request, exc)

Here is exception.py

from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from fastapi import Request

async def validation_exception_handler(request: Request, exc: RequestValidationError):
    error_messages = []
    for error in exc.errors():
        if error["type"] == "value_error.email":
            error_messages.append({"status_code": 422, "detail": "Invalid email address"})
        else:
            error_messages.append({"status_code": 422, "detail": error["msg"]})
    print(error_messages[0])
    return JSONResponse(
        status_code=422,
        content=error_messages[0],
    )

Here is my design pattern publically available
https://github.com/FaizanMunsaf/FastApi-MVC

@sanzoghenzo

This comment was marked as spam.

@codespearhead
Copy link

This PR ( #11176 ) seems to fix it.

@tamird tamird linked a pull request May 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation about how to use FastAPI question Question or problem
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants