CherryPy:"error_page.default"配置与"error_page.404"配置设置有什么区别? [英] CherryPy: what is the difference between `error_page.default` vs. `error_page.404` config settings?

查看:21
本文介绍了CherryPy:"error_page.default"配置与"error_page.404"配置设置有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要显示自己的404&500页,到目前为止,我发现了2种可能性:

1:使用 cherrypy.config.update

  def error_page_404(状态,消息,回溯,版本):返回(找不到错误404页面")def error_page_500(状态,消息,回溯,版本):返回(错误:")cherrypy.config.update({'error_page.404':error_page_404,'error_page.500':error_page_500}) 

  1. 使用 _cp_config :

  from cherrypy import _cperrordef handle_error():cherrypy.response.status = 500cherrypy.log("handle_error()被调用.警报!","WEBAPP")cherrypy.response.body = ['对不起,发生了错误.管理员已收到通知"]错误= _cperror.format_exc()def error_page(状态,消息,回溯,版本):cherrypy.log("error_page()被调用.可能不是很重要.","WEBAPP")返回对不起,发生了一个错误."类根:_cp_config = {'error_page.default':error_page,'request.error_response':handle_error} 

但是有什么区别或建议更可取吗?

解决方案

request.error_response 允许您设置用于处理某些意外错误的处理程序,例如您自己的HTTP处理程序引发的异常.您将为此选项设置的可调用对象将完全不接收任何参数,并且您必须检查 sys.exc_info() 以获得详细信息,以了解发生了什么.您还必须在错误处理程序中明确地自行设置 cherrypy.response.status cherrypy.response.body .

如果要修改HTTP错误代码的错误响应(引发 cherrypy.HTTPError 的实例,例如 racherpypy.NotFound ),则可以使用 error_page.default (全部捕获)或 error_page.404 (特定于错误)来处理这些错误. error_page 选项支持文件路径和可调用值.如果使用文件路径,则HTML模板文件可以使用以下替换模式:%(status)s %(message)s %(追溯和%(version)s .如果您选择使用函数,它将接收那些作为参数( callback(状态,消息,回溯,版本)).然后,该可调用的返回值将用于HTTP响应有效载荷.

如您所见,这些方法具有不同的含义以及不同程度的灵活性和可用性.选择任何适合您的方法.在内部,默认的 request.error_response 使用 error_page 设置来确定要返回的内容.因此,如果您重新定义 request.error_response ,它将不会使用 error_page.* 设置,除非您明确地进行设置.

请参见此处具有某些解释的文档字符串.

Let's say I want to display my own 404 & 500 pages, I've found 2 possibilities so far:

1: Using cherrypy.config.update

def error_page_404(status, message, traceback, version):
    return ('Error 404 Page not found')

def error_page_500(status, message, traceback, version):
    return ('Error:')

cherrypy.config.update({'error_page.404': error_page_404, 'error_page.500': error_page_500})

  1. Using _cp_config:

from cherrypy import _cperror

def handle_error():
    cherrypy.response.status = 500
    cherrypy.log("handle_error() called. Alarm!", "WEBAPP")
    cherrypy.response.body = ['Sorry, an error occured. The admin has been notified']
    error = _cperror.format_exc()

def error_page(status, message, traceback, version):
    cherrypy.log("error_page() called. Probably not very important.", "WEBAPP")
    return "Sorry, an error occured."

class Root: 
     _cp_config = { 
         'error_page.default': error_page, 
         'request.error_response': handle_error 
     } 

but is there a difference or a suggestion which is preferable to use?

解决方案

request.error_response allows you to set a handler for processing of some unexpected errors, like your own exceptions raised from HTTP handlers. The callable that you'll set for this option will receive no arguments at all and you'll have to inspect sys.exc_info() for the details, to find out what happened. You'll also have to set cherrypy.response.status and cherrypy.response.body by yourself, explicitly in your error handler.

If you want to modify the error response for HTTP error codes (when instances of cherrypy.HTTPError are raised, like raise cherrypy.NotFound), you can use error_page.default (catch-all) or error_page.404 (error-specific) for handling those errors. error_page options support both file path and callable values. In case of using a file path, the HTML template file can use the following substitution patterns: %(status)s, %(message)s, %(traceback)s, and %(version)s. If you opt-in to using a function, it'll receive those as arguments (callback(status, message, traceback, version)). The return value of this callable is then used HTTP response payload.

As you can see, these approaches have different implications and different levels of flexibility and usability. Choose whatever works for you. Internally, the default request.error_response uses error_page settings to figure out what to return. So if you redefine request.error_response, it'll not use error_page.* settings unless you explicitly make it do so.

See the docstring with some explanation here.

这篇关于CherryPy:"error_page.default"配置与"error_page.404"配置设置有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆