【问题标题】:Python Pyramid exception_view exception detailsPython金字塔异常_查看异常详情
【发布时间】:2020-07-29 18:35:36
【问题描述】:

我正在使用 Python Pyramid 框架并且正在使用异常视图。

例如

@exception_view_config(TypeError, renderer='json')
def type_error(exc, request):
    logger.info("There was a type error %s", str(exc)

但是,异常对象没有关于错误发生位置的任何有用信息。相反,在回溯中它是excview_tween [tweens.py:43]

有没有办法在这个异常视图中获取更多相关信息?

【问题讨论】:

    标签: python pyramid


    【解决方案1】:

    您需要对 exc 参数做一些事情,无论是记录它还是在视图中返回它。

    class TypeError(Exception):
        def __init__(self, msg):
            self.msg = msg
    
    @exception_view_config(TypeError, renderer='json')
    def type_error(exc, request):
        response =  Response('Type Error: %s' % exc.msg)
        response.status_int = 500
        return response
    

    您需要使用exc 引发异常。

    raise TypeError("Here I am!")
    

    Custom Exception Views

    【讨论】:

    • 对不起,我正在这样做,我已经更新了问题。我的问题是回溯和异常对象不包含有关异常发生位置的任何过于有用的信息。
    • 我想你错过了raise。您可以在此处放置所需的任何信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2020-03-26
    • 1970-01-01
    • 2018-03-30
    • 2010-11-02
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多