Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Discussion options

You must be logged in to vote

I don't think such feature would ever been implemented - it seems not good to me to specify one error message for all possible errors for this parameter.

But you can implement such logic using the following approach:

from fastapi import FastAPI, Query, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

app = FastAPI()


def format_error_msg(err: dict):
    if err["type"] == "string_too_long":
        err["msg"] = f"{err['loc'][-1]} too long"

    if err["type"] == "int_parsing":
        err["msg"] = f"{err['loc'][-1]} need integer"

    return err


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(r…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@binaryantcom
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
4 participants