aws / aws/aws-lambda-python-runtime-interface-client

Error logging doesn't support chained exceptions.

未关闭
#25 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
294
派生
84
平均合并
3 天 7 分钟
30 天内合并 PR
3

描述

The [bootstrap.py](https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/main/awslambdaric/bootstrap.py) wrapper's error logging doesn't correctly handle chained exceptions (see [PEP-3134](https://www.python.org/dev/peps/pep-3134/))

### Example

Here's a simple example of code that uses chained exceptions. In that case, when the second exception is thrown, it will have a reference to the first exception stored in `__cause__` field (basically, it's doing something like `e2.__cause__ = e`).

```python
try:
raise Exception("first!")
except Exception as e:
raise Exception("second!") from e
```

When this code is executed, the error is printed like this.

```
Traceback (most recent call last):
File "/Users/piotr/dev/bazel-fun/python/src/logging_with_cause/test.py", line 83, in
raise Exception("first!")
Exception: first!

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/piotr/dev/bazel-fun/python/src/logging_with_cause/test.py", line 85, in
raise Exception("second!") from e
Exception: second!
```

It includes:
* first exception's message and stacktrace followed by
* `The above exception was the direct cause of the following exception:`, followed by
* second exception's message and stacktrace

### Issue

When a chained exception is thrown inside Lambda however, it only prints second exception:

```
[ERROR] Exception: second!
Traceback (most recent call last):
  File "/Users/piotr/dev/bazel-fun/python/src/logging_with_cause/test.py", line 81, in
    raise Exception("second!") from e
```

### Expected result

The whole chain should be printed. Note - the chain can contain any number of exceptions, not only 2.

One option could be:

```
[ERROR] Exception: second!
Traceback (most recent call last):
  File "/Users/piotr/dev/bazel-fun/python/src/logging_with_cause/test.py", line 81, in
    raise Exception("second!") from e

Caused by: Exception: first!
Traceback (most recent call last):
File "/Users/piotr/dev/bazel-fun/python/src/logging_with_cause/test.py", line 83, in
raise Exception("first!")
...
```

Code that is responsible for logging exceptions is [here](https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/c1c9b2954e5fa3a952be906f32c8e91df8476108/awslambdaric/bootstrap.py#L110).

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。