Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BATTLE.py错误路由_Python_Bottle - Fatal编程技术网

Python BATTLE.py错误路由

Python BATTLE.py错误路由,python,bottle,Python,Bottle,py附带一个导入来处理抛出的HTTPErrors并路由到函数 首先,文档声称我可以(还有几个例子): 但是,当导入此语句时,错误未得到解决,但在运行时,应用程序会忽略此错误,并将我指向“常规错误”页面 我找到了一个解决这个问题的方法: from bottle import Bottle main = Bottle() @Bottle.error(main, 500) def custom500(error): return 'my custom message' 但这段代码阻止我将

py附带一个导入来处理抛出的HTTPErrors并路由到函数

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

但是,当导入此语句时,错误未得到解决,但在运行时,应用程序会忽略此错误,并将我指向“常规错误”页面

我找到了一个解决这个问题的方法:

from bottle import Bottle

main = Bottle()

@Bottle.error(main, 500)
def custom500(error):
    return 'my custom message'
但这段代码阻止我将所有错误都嵌入到一个单独的模块中,以控制如果我将它们保留在main.py模块中可能会出现的问题,因为第一个参数必须是一个瓶子实例

所以我的问题是:

  • 还有其他人经历过吗

  • 为什么错误似乎只有在我的情况下才能解决(我是从pip安装瓶子安装的)

  • 是否有一种无缝的方法将我的错误路由从单独的python模块导入主应用程序

  • 这对我很有用:

    from bottle import error, run, route, abort
    
    @error(500)
    def custom500(error):
        return 'my custom message'
    
    @route("/")
    def index():
        abort("Boo!")
    
    run()
    

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

    error.py

    app.py


    在某些情况下,我发现最好将瓶子子类化。下面是一个这样做并添加自定义错误处理程序的示例

    #!/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)
    
    def custom500(error):
        return 'my custom message'
    
    handler = {
        500: custom500,
    }
    
    from bottle import *
    import error
    
    app = Bottle()
    app.error_handler = error.handler
    
    @app.route('/')
    def divzero():
        return 1/0
    
    run(app)
    
    #!/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)