AWS Lambda在Python 3.8中不显示原因 异常堆栈跟踪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AWS Lambda在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
为什么不是 direct cause
例外 from
语句显示在日志中?
您提到的行为是意料之中的,我也看到了同样的情况。似乎 Lambda 目前不支持链式异常。不过,要解决这个问题,您可以添加自己的日志记录器来捕获异常。
例如,使用 回溯 来检索异常堆栈。
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,并引发一个通用错误,使程序终止。例子:这将产生 CloudWatch 的输出,如以下所示。
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这样的输出
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
这样我们就不用再做这样的变通工作了。
以上是关于AWS Lambda在Python 3.8中不显示原因 异常堆栈跟踪的主要内容,如果未能解决你的问题,请参考以下文章
SQL服务器连接在lambda容器中工作正常,但是在将zip上传到aws lambda后失败
AWS java sdk 1.10.2 中不存在包 com.amazonaws.services.lambda.runtime
AWS lambda:如果log4j.xml中不存在环境变量,有没有办法设置默认值?
Python(3.8):map(lambda x:x.method(),list_of_objects)不起作用[重复]