Skip to content

ExceptionRenderer w/ ExceptionDictTransformer unable to handle exception log outside exception handler #634

@raqbit

Description

@raqbit

When using the exception log method outside an exception handler, the ExceptionDictTransformer raises an AttributeError as it calls extract, which does not handle the case where the given exception type is None.

While according to the Python docs1 this method should only be used from exception handlers, I ran into this issue as the aio-pika (aiormq) library uses it outside an exception handler (issue). (I have configured structlog as the formatter for all loggers, using this setup)

Note that the default _format_exception formatter handles this case properly, as it handles (None, None, None) exc_info2.


Reproduction:

import structlog
print(structlog.__version__) # 24.4.0
structlog.configure([structlog.processors.dict_tracebacks])
structlog.get_logger().exception("oh no") # AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?

Footnotes

  1. https://docs.python.org/3/library/logging.html#logging.Logger.exception

  2. https://github.com/hynek/structlog/blob/e48a553979b00ab580d8ab0e55b5cf96e752a324/src/structlog/_frames.py#L25

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions