任何异常的全局错误处理程序 [英] Global error handler for any exception
问题描述
我无法使用 got_request_exception
信号,因为它不允许修改响应( http://flask.pocoo.org/docs/0.10/signals/ )。
相比之下所有信号处理程序都以未定义的顺序执行,并且不会修改任何数据。
我宁愿不包含 app.handle_exception
函数感觉像内部API。我想我是这样的:
@ app.errorhandler()
def handle_global_error(e):
返回全局错误
请注意 errorhandler
不采取任何参数,这意味着它会捕获所有异常/状态代码,它们没有附加一个特定的错误处理程序。我知道我可以使用 errorhandler(500)
或 errorhandler(Exception)
来捕获异常,但如果我执行 abort(409)
例如,它仍然会返回HTML响应。
您可以使用 @ app.errorhandler(Exception)
:
演示(HTTPException检查确保状态代码保存):
从烧瓶导入Flask,abort,jsonify
从werkzeug.exceptions import HTTPException
app = Flask('test')
@ app.errorhandler(异常)
def handle_error(e):
code = 500
if isinstance (e,HTTPException):
code = e.code
return jsonify(error = str(e)),代码
@ app.route('/')
def index():
abort(409)
app.run(port = 1234)
输出:
$ http get http://127.0.0.1:1234/
$ p $对于较旧的Flask版本(<= 0.10.1,即当前的任何非git / master版本),对于较旧的Flask版本,请将以下代码添加到应用程序中以注册HTTP错误显式地:
HTTP / 1.0 409 CONFLICT
内容长度:31日期:2010年3月29日,星期日17:06:54 GMT
服务器:Werkzeug / 0.10.1 Python / 3.4.3
{
error:409:Conflict
}
$ http get http://127.0.0.1:1234/notfound
HTTP / 1.0 404 NOT FOUND
内容长度:32
内容类型:application / json
日期:太阳,2015年3月29日17:06:58 GMT
服务器:Werkzeug / 0.10。 1 Python / 3.4.3
{
错误:404:找不到
}
从werkzeug导入HTTP_STATUS_CODES
在HTTP_STATUS_CODES中的代码:
app.register_error_handler(code,handle_error )
Is there a way to add a global catch-all error handler in which I can change the response to a generic JSON response?
I can't use the
got_request_exception
signal, as it is not allowed to modify the response (http://flask.pocoo.org/docs/0.10/signals/).In contrast all signal handlers are executed in undefined order and do not modify any data.
I would prefer to not wrap the
app.handle_exception
function as that feels like internal API. I guess I'm after something like:@app.errorhandler() def handle_global_error(e): return "Global error"
Note the
errorhandler
does not take any parameters, meaning it would catch all exceptions/status codes which does not have a specific error handler attached to them. I know I can useerrorhandler(500)
orerrorhandler(Exception)
to catch exceptions, but if I doabort(409)
for example, it will still return a HTML response.解决方案You can use
@app.errorhandler(Exception)
:Demo (the HTTPException check ensures that the status code is preserved):
from flask import Flask, abort, jsonify from werkzeug.exceptions import HTTPException app = Flask('test') @app.errorhandler(Exception) def handle_error(e): code = 500 if isinstance(e, HTTPException): code = e.code return jsonify(error=str(e)), code @app.route('/') def index(): abort(409) app.run(port=1234)
Output:
$ http get http://127.0.0.1:1234/ HTTP/1.0 409 CONFLICT Content-Length: 31 Content-Type: application/json Date: Sun, 29 Mar 2015 17:06:54 GMT Server: Werkzeug/0.10.1 Python/3.4.3 { "error": "409: Conflict" } $ http get http://127.0.0.1:1234/notfound HTTP/1.0 404 NOT FOUND Content-Length: 32 Content-Type: application/json Date: Sun, 29 Mar 2015 17:06:58 GMT Server: Werkzeug/0.10.1 Python/3.4.3 { "error": "404: Not Found" }
For older Flask versions (<=0.10.1, i.e. any non-git/master version at the moment), add the following code to your application to register the HTTP errors explicitly:
from werkzeug import HTTP_STATUS_CODES for code in HTTP_STATUS_CODES: app.register_error_handler(code, handle_error)
这篇关于任何异常的全局错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!