Skip to content

TRY400 flags log.error() + raise/sys.exit() #4136

@jankatins

Description

@jankatins

In the following code snippet, a TRY400 violation is raised because I use log.error() and not log.exception() in an exception handler. In this case it's by design as I immediately re-raise the exception, I just want to add some message to the exception and I don't want the stacktrace shown twice.

import logging
log = logging.get_logger(__name__)
# The outer app loop...
try:
    # actually some function call which can't handle the error but wants to abort...
    try:
        raise Exception("bad")
    except Exception:
        # to get some info into the log to "enrich" it...
        # and I don't want to see the exception stacktrace twice
        log.error("That might mean ..." ) # noqa: TRY400: use log.exception()
        raise
except Exception:
    log.exception("Caught unhandled exception in outer context")

In my opinion the pattern "in except block, with a log.something + 'raise' in all cases` should be something TRY400 should ignore/be ok with.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionAwaiting a decision from a maintainerruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions