Skip to content

Error logging doesn't support chained exceptions. #25

@pimlock

Description

@pimlock

The bootstrap.py wrapper's error logging doesn't correctly handle chained exceptions (see 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).

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 <module>
    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 <module>
    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 <module>
    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 <module>
    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 <module>
    raise Exception("first!")
...

Code that is responsible for logging exceptions is here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions