Skip to main content
 首页 » 编程设计

python之如何仅打印传递给 python flask 中的错误处理程序函数的异常的简单回溯

2024年10月24日18powertoolsteam

我有一些用 python flask 编写的代码,其中我有一个函数如下:

@app.errorhandler(500) 
def internal_error(exception): 
    print "500 error caught" 

但此消息不足以为我提供足够的信息。我只想打印传递给错误处理程序的异常的回溯。有什么办法可以做到这一点?

请您参考如下方法:

假设当sys 中的异常和回溯仍然可用时,错误处理程序是从上下文中调用的,您应该能够使用traceback.format_exc。 .

import traceback 
 
@app.errorhandler(500) 
def internal_error(exception): 
    print "500 error caught" 
    print traceback.format_exc()