AWS Lambda Python 3.7 runtime exception logging

☆樱花仙子☆ 提交于 2019-12-03 05:38:22

Yep, I've noticed it. To overcome I use a decorator.

def log_errors(func: Callable[[dict, dict], None]):
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except Exception as err:
            warning(traceback.format_exc())
            raise err

    return wrapper

Usage:

@log_errors
def handler(event, context):
...

This is a bug in AWS Lambda. I noticed it mid-December and filed an issue; the response included the following: "I would like to let you know that this has been identified as a genuine issue and the Lambda team is currently working towards resolving it. But, unfortunately I do not have an ETA on when this issue will be resolved."

My solution was to revert back to python3.6 runtime until Amazon fixes it.

It is strange because Exception is built-in function, which should work on both 3.6 and 3.7.

As a workaround, I would suggest making custom exception. Here is a simple example.

test.py

class Error(Exception):
    pass

class CustomException(Error):
    pass

raise CustomException("I failed")

Here is a result when running.

TK$ python3 test.py 

Traceback (most recent call last):
 File "test.py", line 7, in <module>
    raise CustomException("I failed")
__main__.CustomException: I failed
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!