flask-pydantic icon indicating copy to clipboard operation
flask-pydantic copied to clipboard

when response_many is set, can't handle returning errors

Open yfried opened this issue 2 years ago • 2 comments

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.

yfried avatar Jan 05 '22 11:01 yfried

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.

bauerji avatar Jan 05 '22 15:01 bauerji

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

Merinorus avatar Oct 17 '22 14:10 Merinorus