Bottle.py错误路由 [英] Bottle.py error routing

查看:43
本文介绍了Bottle.py错误路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bottle.py附带了一个导入,用于处理引发HTTPError并路由到函数的情况.

Bottle.py ships with an import to handle throwing HTTPErrors and route to a function.

首先,文档声称我可以(并且可以举几个例子):

Firstly, the documentation claims I can (and so do several examples):

from bottle import error

@error(500)
def custom500(error):
    return 'my custom message'

但是,在导入此语句时,无法解决错误,但是在运行应用程序时,它将忽略此错误,只是将我定向到一般错误页面.

however, when importing this statement error is unresolved but on running the application ignores this and just directs me to the generic error page.

我找到了一种解决此问题的方法:

I found a way to get around this by:

from bottle import Bottle

main = Bottle()

@Bottle.error(main, 500)
def custom500(error):
    return 'my custom message'

但是此代码阻止我将错误全部嵌入到单独的模块中,以控制将错误保存在main.py模块中时所产生的混乱,因为第一个参数必须是bottle实例.

But this code prevents me from embedding my errors all in a separate module to control the nastiness that would ensue if I kept them in my main.py module because the first argument has to be a bottle instance.

所以我的问题:

  1. 其他人有没有经历过?

  1. Has anyone else experienced this?

为什么错误似乎仅在我的情况下解决(我是从 pip install bottle 安装的)?

why doesn't error seem to resolve in only my case (I installed from pip install bottle)?

是否有无缝方法将错误路由从单独的python模块导入到主应用程序中?

Is there a seamless way to import my error routing from a separate python module into the main application?

推荐答案

如果要将错误嵌入到另一个模块中,则可以执行以下操作:

If you want to embed your errors in another module, you could do something like this:

error.py

def custom500(error):
    return 'my custom message'

handler = {
    500: custom500,
}

app.py

from bottle import *
import error

app = Bottle()
app.error_handler = error.handler

@app.route('/')
def divzero():
    return 1/0

run(app)

这篇关于Bottle.py错误路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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