Flask error handling: "Response object is not iterable" Flask error handling: "Response object is not iterable" flask flask

Flask error handling: "Response object is not iterable"


from flask import Flask, jsonifyapp = Flask(__name__)@app.errorhandler(404)def not_found(error):    return jsonify({'error':'not found'}), 404app.run()

With code above, curl http://localhost:5000/ give me:

{  "error": "not found"}

Are you using flask.jsonify?


As mentioned in the comments for the previous answer, that code isn't supported on Flask 0.8, and would require 0.9 or higher. If you need to support Flask 0.8, here is a compatible version that assigns the "status_code" instead:

@app.errorhandler(404)def not_found(error):    resp = jsonify({'error':'not found'})    resp.status_code = 404    return resp