[英]AWS Lambda not showing cause Exception stacktrace in Python 3.8
我使用运行时 Python 3.8 将以下代码部署到 AWS Lambda。
try:
raise Exception('my exception')
except Exception as e:
raise ValueError('my exception 2') from e
在 CloudWatch 中,我希望看到这样的异常链:
Traceback (most recent call last):
File "/var/task/handler.py", line 2, in <module>
raise Exception('my exception')
Exception: my exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/var/task/handler.py", line 4, in <module>
raise ValueError('my exception 2') from e
ValueError: my exception 2
相反,我只看到 CloudWatch 中报告的第一个异常,如下所示:
[ERROR] ValueError: my exception 2
Traceback (most recent call last):
File "/var/task/handler.py", line 21, in pevm_import_budget_file
raise ValueError('my exception 2') from e
为什么日志中没有显示from
语句的direct cause
异常?
您提到的行为是预期的,我也看到了同样的情况。 Lambda 目前似乎不支持链式异常。 但是,为了解决这个问题,您可以添加自己的记录器来捕获异常。
例如,使用traceback检索异常堆栈:
import traceback
def lambda_handler(event, context):
try:
try:
raise Exception('my exception')
except Exception as e1:
raise ValueError('my exception 2')
except Exception as e2:
traceback.print_exception(type(e2), value=e2, tb=e2.__traceback__)
return {}
CloudWatch 日志如下所示:
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 6, in lambda_handler
raise Exception('my exception')
Exception: my exception
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 8, in lambda_handler
raise ValueError('my exception 2')
ValueError: my exception 2
我更喜欢的另一个解决方案是将堆栈跟踪打印到 stderr 并引发一般错误,以便程序终止。 例子:
import logging, traceback, sys
class ProgramError(Exception):
def __init__(self, msg='There should be more of exception traceback above.', **kwargs):
super(ProgramError, self).__init__(msg, **kwargs)
def log_exception():
exc_type, exc_value, exc_traceback = sys.exc_info()
logging.error(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
try:
raise Exception('my exception')
except Exception as e:
log_exception()
raise ProgramError()
这会生成 CloudWatch output,如下所示:
2020-06-02T06:08:57.340000+00:00 2020/06/02/[$LATEST]df0f5bb977b443f9889a809f0d1affa4 START RequestId: e609f118-75d9-4cc7-81fe-44036d492814 Version: $LATEST
2020-06-02T06:08:57.341000+00:00 2020/06/02/[$LATEST]df0f5bb977b443f9889a809f0d1affa4 [ERROR] 2020-06-02T06:08:57.341Z e609f118-75d9-4cc7-81fe-44036d492814 Traceback (most recent call last):
File "/var/task/handler.py", line 35, in testfile
raise Exception('my exception')
Exception: my exception
2020-06-02T06:08:57.342000+00:00 2020/06/02/[$LATEST]df0f5bb977b443f9889a809f0d1affa4 [ERROR] ProgramError: There should be more of exception traceback above.
Traceback (most recent call last):
File "/var/task/handler.py", line 38, in testfile
raise ProgramError()
2020-06-02T06:08:57.343000+00:00 2020/06/02/[$LATEST]df0f5bb977b443f9889a809f0d1affa4 END RequestId: e609f118-75d9-4cc7-81fe-44036d492814
2020-06-02T06:08:57.343000+00:00 2020/06/02/[$LATEST]df0f5bb977b443f9889a809f0d1affa4 REPORT RequestId: e609f118-75d9-4cc7-81fe-44036d492814 Duration: 2.71 ms Billed Duration: 100 ms Memory Size: 256 MB Max Memory Used: 95 MB Init Duration: 1297.82 ms
如果有一连串异常导致Exception
,它们将被列出并由The above exception was the direct cause of the following exception:
.
感谢@Paradigm 对此的启发! 希望 AWS 很快会修复对from
支持,这样我们就不必做这样的变通方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.