abort(404) 是怎么变成一个 HTTP 响应的?错误页长什么样?能带自己的头、正文吗?不认识的状态码会怎样?这篇逐个试。
404 NOT FOUND
Content-Type: text/html; charset=utf-8
Content-Length: 207
正文: <!doctype html> | <html lang=en> | <title>404 Not Found</title> | <h1>Not Found</h1> | <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>上一篇:异常处理器的匹配顺序;下一篇:make_response() 的响应转换过程。
404 NOT FOUND | <!doctype html> | <html lang=en> | <title>404 Not Found</title> | <h1>Not Found</h1> | <p>文章 42 不存在</p> 捕获到的异常: NotFound | code = 404 | name = Not Found | description = 文章 42 不存在
429: 429 TOO MANY REQUESTS | Retry-After = 30 GET 一个只允许 POST 的路由: 405 METHOD NOT ALLOWED | Allow = OPTIONS, POST 401: 401 UNAUTHORIZED | WWW-Authenticate = Basic realm=lab
abort(Response(...)): 418 I'M A TEAPOT | 自定义响应体 | X-Why = 茶壶
abort(redirect("/elsewhere")): 302 FOUND | Location = /elsewhere/after: 400 BAD REQUEST abort(498) → LookupError: no exception for 498
default_exceptions 共 31 个状态码;其中 4xx 25 个、5xx 6 个
部分: {400: 'BadRequest', 401: 'Unauthorized', 403: 'Forbidden', 404: 'NotFound', 405: 'MethodNotAllowed', 409: 'Conflict', 410: 'Gone', 413: 'RequestEntityTooLarge', 415: 'UnsupportedMediaType', 418: 'ImATeapot', 422: 'UnprocessableEntity', 429: 'TooManyRequests', 500: 'InternalServerError', 501: 'NotImplemented', 503: 'ServiceUnavailable'}
type(current_app.aborter) = Aborter | 是否是 app.aborter: True
事先往 app.aborter.mapping 里注册了 499 之后,abort(499): 499 UNKNOWN | <!doctype html> | <html lang=en> | <title>499 Unknown Error</title> | <h1>Unknown Error</h1> | <p>客户