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

when response_many is set, can't handle returning errors #46

Open
yfried opened this issue Jan 5, 2022 · 2 comments
Open

when response_many is set, can't handle returning errors #46

yfried opened this issue Jan 5, 2022 · 2 comments

Comments

@yfried
Copy link

yfried commented Jan 5, 2022

When a route that has repononse_many wants to return a proper error code on a, flask-pydantic crashes with

This should return a 400 error

@app.route("/<myid>", methods=["GET"])
@pydantic_validate(response_many=True)
def return_many(myid: str):
    if not_in_db(myid):
        return jsonify({"success": False, "message": f"{myid} not found in DB"}), 404
....

But instead, the server breaks and raises a exception:

flask_pydantic.exceptions.InvalidIterableOfModelsException: (<Response 86 bytes [200 OK]>, 404)

There should be some way to describe the error responses that shouldn't be a an array.

@bauerji
Copy link
Owner

bauerji commented Jan 5, 2022

I think this is not the right approach how to handle exceptions in Flask. I suggest you to read Error Handlers. That way you route handler just raises exception and your error handler creates JSON response with proper status code.

@Merinorus
Copy link
Contributor

I would need also the possibility to define specific errors message in each route...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants