python-flask 处理应用程序错误
python-flask handling application errors
我构建了一个 flask 应用程序,我尝试使用 errorhandler 装饰器从我的路由中捕获未处理的应用程序错误。
我有一个 main.py 看起来像这样,
app = Flask(__name__)
api = Api(app)
api.add_resource(Ping, '/ping')
@app.errorhandler(500)
def internal_server_error(error):
print "caught internal server error"
return "This page does not exist", 500
路由 Ping 在另一个文件中,这里是文件的示例版本
class Ping(Resource):
def get(self):
raise
return {}, 200
我已经加注以尝试重现 500 内部服务器错误。这是我的应用程序引发的示例错误
[2019-01-26 10:37:36,449] ERROR in app: Exception on /events/v1/monitoring/ping [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/myapi/ping.py", line 17, in get
raise
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
127.0.0.1 - - [26/Jan/2019 10:37:36] "GET /ping HTTP/1.0" 500 -
由于我无法弄清楚的原因,@app.errorhandler 装饰器没有捕获应用程序引发的任何 500 错误。
试试这个:
@app.errorhandler(Exception)
我认为您正在使用 flask-restful。为了捕获错误处理程序,您应该在配置文件中将配置参数 PROPAGATE_EXCEPTIONS
设置为 True
。
Flask restful 有自己的错误处理程序,这与 Flask 的不同。这是摘录:
[...]
if not isinstance(e, HTTPException) and current_app.propagate_exceptions:
exc_type, exc_value, tb = sys.exc_info()
if exc_value is e:
raise
else:
raise e
headers = Headers()
if isinstance(e, HTTPException):
code = e.code
default_data = {
'message': getattr(e, 'description', http_status_message(code))
}
headers = e.get_response().headers
else:
code = 500
default_data = {
'message': http_status_message(code),
}
[...]
将 PROPAGATE_EXCEPTIONS
设置为 True
后,您应该至少添加以下两个错误处理程序:
@app.errorhandler(Exception)
def internal_server_error(e):
return jsonify({'msg': 'Internal server error'}), 500
@app.errorhandler(500)
def internal_server_error_500(e):
return jsonify({'msg': 'Internal server error'}), 500
因此,第一个将捕获在应用程序级别引发的任何异常,甚至是您自己的自定义异常,第二个将捕获任何 500 错误,例如在 abort(500)
.
上引发的
我构建了一个 flask 应用程序,我尝试使用 errorhandler 装饰器从我的路由中捕获未处理的应用程序错误。
我有一个 main.py 看起来像这样,
app = Flask(__name__)
api = Api(app)
api.add_resource(Ping, '/ping')
@app.errorhandler(500)
def internal_server_error(error):
print "caught internal server error"
return "This page does not exist", 500
路由 Ping 在另一个文件中,这里是文件的示例版本
class Ping(Resource):
def get(self):
raise
return {}, 200
我已经加注以尝试重现 500 内部服务器错误。这是我的应用程序引发的示例错误
[2019-01-26 10:37:36,449] ERROR in app: Exception on /events/v1/monitoring/ping [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib64/python2.7/site-packages/flask_restful/__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/myapi/ping.py", line 17, in get
raise
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType
127.0.0.1 - - [26/Jan/2019 10:37:36] "GET /ping HTTP/1.0" 500 -
由于我无法弄清楚的原因,@app.errorhandler 装饰器没有捕获应用程序引发的任何 500 错误。
试试这个:
@app.errorhandler(Exception)
我认为您正在使用 flask-restful。为了捕获错误处理程序,您应该在配置文件中将配置参数 PROPAGATE_EXCEPTIONS
设置为 True
。
Flask restful 有自己的错误处理程序,这与 Flask 的不同。这是摘录:
[...]
if not isinstance(e, HTTPException) and current_app.propagate_exceptions:
exc_type, exc_value, tb = sys.exc_info()
if exc_value is e:
raise
else:
raise e
headers = Headers()
if isinstance(e, HTTPException):
code = e.code
default_data = {
'message': getattr(e, 'description', http_status_message(code))
}
headers = e.get_response().headers
else:
code = 500
default_data = {
'message': http_status_message(code),
}
[...]
将 PROPAGATE_EXCEPTIONS
设置为 True
后,您应该至少添加以下两个错误处理程序:
@app.errorhandler(Exception)
def internal_server_error(e):
return jsonify({'msg': 'Internal server error'}), 500
@app.errorhandler(500)
def internal_server_error_500(e):
return jsonify({'msg': 'Internal server error'}), 500
因此,第一个将捕获在应用程序级别引发的任何异常,甚至是您自己的自定义异常,第二个将捕获任何 500 错误,例如在 abort(500)
.