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

本文是小编为大家收集整理的关于Bottle.py的错误路由的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

botter.py船只,进口用于处理扔httperrors和途径.

首先,文档声称我可以(还有几个示例):

from bottle import error

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

但是,在导入此语句的时候未解决,但是在运行应用程序时,忽略了这一点,只是将我引向通用错误页面.

我找到了一种通过以下方式解决这个问题的方法

from bottle import Bottle

main = Bottle()

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

但是,此代码使我无法将错误嵌入到一个单独的模块中,以控制如果我将它们保存在Main.py模块中,则会随之而来,因为第一个参数必须是瓶子实例.

.

所以我的问题:

  1. 其他人是否经历过?

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

  3. 是否有一种无缝的方法可以将我的错误路由从单独的Python模块导入到主应用程序中?

推荐答案

如果要将错误嵌入另一个模块,则可以执行这样的操作:

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)

其他推荐答案

这对我有用:

from bottle import error, run, route, abort

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

@route("/")
def index():
    abort("Boo!")

run()

其他推荐答案

在某些情况下,我发现亚班级瓶最好.这是这样做并添加自定义错误处理程序的示例.

#!/usr/bin/env python3
from bottle import Bottle, response, Route

class MyBottle(Bottle):
    def __init__(self, *args, **kwargs):
        Bottle.__init__(self, *args, **kwargs)
        self.error_handler[404] = self.four04
        self.add_route(Route(self, "/helloworld", "GET", self.helloworld))
    def helloworld(self):
        response.content_type = "text/plain"
        yield "Hello, world."
    def four04(self, httperror):
        response.content_type = "text/plain"
        yield "You're 404."

if __name__ == '__main__':
    mybottle = MyBottle()
    mybottle.run(host='localhost', port=8080, quiet=True, debug=True)

本文地址:https://www.itbaoku.cn/post/1975348.html

问题描述

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'

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.

So my questions:

  1. Has anyone else experienced this?

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

  3. 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)

其他推荐答案

This works for me:

from bottle import error, run, route, abort

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

@route("/")
def index():
    abort("Boo!")

run()

其他推荐答案

In some cases I find it's better to subclass Bottle. Here's an example of doing that and adding a custom error handler.

#!/usr/bin/env python3
from bottle import Bottle, response, Route

class MyBottle(Bottle):
    def __init__(self, *args, **kwargs):
        Bottle.__init__(self, *args, **kwargs)
        self.error_handler[404] = self.four04
        self.add_route(Route(self, "/helloworld", "GET", self.helloworld))
    def helloworld(self):
        response.content_type = "text/plain"
        yield "Hello, world."
    def four04(self, httperror):
        response.content_type = "text/plain"
        yield "You're 404."

if __name__ == '__main__':
    mybottle = MyBottle()
    mybottle.run(host='localhost', port=8080, quiet=True, debug=True)