使用B捕获mako运行时错误

2024-10-06 12:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法来捕捉mako运行时错误使用瓶子。在

使用以下代码捕获python中的运行时错误:

# main.py
from lib import errors
import bottle

app = bottle.app()
app.error_handler = errors.handler
...

# lib/errors.py
from bottle import mako_template as template

def custom500(error):
    return template('error/500')

handler = {
    500: custom500
}

这是完美的,因为异常会变成500个内部服务器错误。在

我想用类似的方式捕捉mako运行时的错误,有人知道如何实现这一点吗?在


Tags: 方法frompyimportapp瓶子bottlelib
1条回答
网友
1楼 · 发布于 2024-10-06 12:37:49

您想要捕获mako.exceptions.SyntaxException。在

这个代码适用于我:

@bottle.route('/hello')
def hello():
    try:
        return bottle.mako_template('hello')

    except mako.exceptions.SyntaxException as exx:
        return 'mako exception: {}\n'.format(exx)

编辑:根据您的评论,这里有一些关于如何在全局范围内安装的提示。安装一个bottle plugin,它将函数包装在mako.exceptions.SyntaxException试试布洛克。在

大致如下:

^{pr2}$

相关问题 更多 >