<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HTTP 错误 &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/http-%e9%94%99%e8%af%af/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Sat, 15 Nov 2025 10:55:15 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>HTTP 错误 &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Flask 全局错误处理（一个函数处理所有 HTTP 错误）</title>
		<link>https://greyli.com/flask-global-error-handler/</link>
		<comments>https://greyli.com/flask-global-error-handler/#respond</comments>
		<pubDate>Sat, 15 Dec 2018 01:07:29 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask 错误处理]]></category>
		<category><![CDATA[HTTP 错误]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2045</guid>
		<description><![CDATA[在 Flask 程序中，使用 app.errorhandler() 装饰器可以注册错误处理函数，传入 HTTP [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	在 Flask 程序中，使用 app.errorhandler() 装饰器可以注册错误处理函数，传入 HTTP 错误状态码或是特定的异常类：
</p>
<pre>
<code>@app.errorhandler(404)
def error_404(e):
    return &#39;404 Error&#39;, 404</code>
</pre>
<p>
	如果发生 404 错误，就会触发这个函数获取返回值作为响应主体。
</p>
<p>
	通常我们会为不同的 HTTP 错误编写各自的的错误处理函数，以便返回不同的响应。如果你愿意的话，我们也可以编写一个统一的错误处理函数，这个函数会处理所有的 HTTP 错误和一般异常，只需要在装饰器内传入 Exception 类即可：
</p>
<pre>
<code>@app.errorhandler(Exception)
def all_exception_handler(e):
    return &#39;Error&#39;, 500</code>
</pre>
<p>
	现在所有的 HTTP 错误都会触发这个函数。你也可以在函数中对错误进行分类处理：
</p>
<pre>
<code>@app.errorhandler(Exception)
def all_exception_handler(e):
    # 对于 HTTP 异常，返回自带的错误描述和状态码
    # 这些异常类在 Werkzeug 中定义，均继承 HTTPException 类
    if isinstance(e, HTTPException):
        return e.desciption, e.code
    return &#39;Error&#39;, 500  # 一般异常</code></pre>
<p>
	如果你使用 Flask 0.12 版本，则需要参考<a href="https://stackoverflow.com/a/44083675/5511849">这个 SO 回答</a>重写相关方法。
</p>
<p>
	附注一些关于错误处理的小知识：
</p>
<ul>
<li>
		对于一般的程序异常（比如 NameError），如果没有特定的异常处理函数，默认都会触发 500 错误处理函数。
	</li>
<li>
		开启调试模式的时候，500 错误会显示错误调试页面。
	</li>
<li>
		500 错误发生时传入错误处理函数的是真正的异常对象，不是 Werkzeug 内置的 HTTP 异常类。
	</li>
<li>
		内置的 HTTP 异常类的 description 和 code 属性分别返回错误描述和状态码。
	</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/flask-global-error-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
