bauerji / flask-pydantic

flask extension for integration with the awesome pydantic package
MIT License
352 stars 56 forks source link

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

Open yfried opened 2 years ago

yfried commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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