对于Django中间件类,process_request如何正常工作,但是process_exception不是调用? [英] For a Django middleware class, how can process_request work just fine, but process_exception not be calls?
问题描述
我在Django中创建了自己的中间件类,直到最近才工作得很好。奇怪的是,process_request仍然被调用很好,但即使响应是500 - 内部服务器错误,也不会调用process_exception。为什么?
无论我将中间件类声明为设置文件中安装的中间件列表中的第一个还是最后一个条目,都没有区别。
谢谢,
Dave
process_exception
仅在视图引发异常
。如评论中所述
如果视图引发异常,请通过异常中间件运行,
,如果异常中间件返回一个响应,使用它。
否则,重新引发异常。
配置错误引起的异常,导入错误, process_request
和 process_view
不能被捕获,并提供给 process_exception
处理程序。
要测试您的 process_exception
是否有效,请确保视图中的异常可以正常工作。 p>
process_request
和 process_exception
之间没有直接的关系,他们是不同目的的处理程序,并在不同阶段被调用。任何异常在 process_request
执行成功并在视图之前引发,不会被 process_exception
被捕获和处理说。
I've created my own middleware class in Django that was working just fine until recently. The strange thing is that process_request still gets called just fine, but even when the response is 500 - internal server error, process_exception is not called at all. Why?
It makes no difference whether I declare my middleware class as the first or the last entry in the list of installed middleware in the settings file.
Thanks, Dave
The process_exception
only gets invoked when the view raises an Exception
. As says in the comment
If the view raised an exception, run it through exception middleware,
and if the exception middleware returns a response, use that.
Otherwise, reraise the exception.
Exceptions raised by misconfiguration, importing error, process_request
and process_view
cannot be caught and feed to process_exception
handlers.
To test whether your process_exception
works, raise an Exception in the view after you ensure it works well.
There is not direct relationship between process_request
and process_exception
, they are handlers for different purposes and get invoked at different stages. Any Exception been raised after the process_request
that executed successfully and before the view, will not be caught and processed by process_exception
as said.
这篇关于对于Django中间件类,process_request如何正常工作,但是process_exception不是调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!