向烧瓶添加自定义HTTP错误和异常

时间:2020-09-27 07:24:08

标签: http flask werkzeug

如何添加自定义HTTP错误?据我了解,可以将6XX和7XX类中的错误代码用于该目的,但是Flask不允许我处理这些错误,因为它说this(601)不是可识别的HTTP错误。

编辑: 这是正确的代码:-

from werkzeug.exceptions import HTTPException

@errors.errorhandler(HTTPException)
def error_601(HTTPException):
    return render_template('errors/601.html'), 601


class No_results_found(HTTPException):
    code = 601
    description = '<p>No_results_found.</p>'

errors.register_error_handler(No_results_found, error_601)

1 个答案:

答案 0 :(得分:1)

请在@app.errorhandler(HTTPException)之前添加error_601

文档:https://flask.palletsprojects.com/en/1.1.x/errorhandling/#registering