前言
flask 运行请求出现异常时,会先触发对应的异常钩子,比如出现404时,会根据NotFound 异常类返回404状态码。
我们也可以根据捕获异常钩子errorhandler 来自定义异常的输出。
404 NotFound
以404 NotFound 为例,在werkzeug.exceptions中可以找到
class NotFound(HTTPException):
"""*404* `Not Found`
Raise if a resource does not exist and never existed.
"""
code = 404
description = (
"The requested URL was not found on the server. If you entered"
" the URL manually please check your spelling and try again."
)
NotFound 类继承了一个基类HTTPException
class HTTPException(Exception):
"""The base class for all HTTP exceptions.