[英]how to catch all exceptions raised in flask_restful app
我确实有Flask-Restful的简单宁静应用程序
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
...
api = Api(app)
api.add_resource(ContactList, "/contacts")
if __name__ == '__main__':
from object_SQLAlchemy import db
db.init_app(app)
app.run(port=5000)
class Contact(Resource):
parser = reqparse.RequestParser()
parser.add_argument(
'contact_no',
type=str,
required=True,
help="This field cannot be left blank"
)
@throttling.Throttle("10/m", strategy=2)
def get(self, name):
contact = Contacts.findbyname(name)
if contact:
return contact.json()
return {"message": "Contact does not exist."}, 404
我的节流实现( https://github.com/scgbckbone/RESTAPI/blob/master/resources/utils/throttling.py )装饰了'get'方法。 重要的是节流装饰器在某些情况下会引发异常-最重要的是在达到限制时。 我希望能够捕获该异常并返回一些合理的json消息。
但以下任何一项均无效:
from ..app_alchemy import api, app
@api.errorhandler(Exception)
def handle_error(e):
return {"error": str(e)}
@app.errorhandler(500)
def handle_error_app(e):
return {"error": str(e.args[0])}
@app.handle_exception(Exception)
def handle_it_app(e):
return {"error": str(e.args[0])}
@api.handle_exception(Exception)
def handle_it(e):
return {"error": str(e.args[0])}
我仍在找回默认信息
{"message": "Internal Server Error"}
我是否正确使用错误处理程序,还是该问题与装饰器的使用有关? 我真的不知道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.