Skip to content

Add recipe to clone an exception argument #141

@ppkarwasz

Description

@ppkarwasz

What problem are you trying to solve?

When logging exceptions using SLF4J or Log4j API, a common mistake is to add a placeholder for the exception in the message format:

try {
    ...
} catch (Exception e) {
    log.error("An error occurred: {}", e);
}

Describe the solution you'd like

Currently there is a CompleteExceptionLogging rewrite rule that deals with the case:

try {
    ...
} catch (Exception e) {
    log.error("An error occurred: {}", e.getMessage());
}

This rule could be expanded to rewrite the first example into either:

try {
    ...
} catch (Exception e) {
    log.error("An error occurred:", e);
}

or

try {
    ...
} catch (Exception e) {
    log.error("An error occurred: {}", e.getMessage(), e);
}

Have you considered any alternatives or workarounds?

Palantir already offers a LoggerInterpolationConsumesThrowable Error Prone rule, that could be expanded to generate a code change suggestion.

Are you interested in contributing this feature to OpenRewrite?

Right now I don't have time for the OpenRewrite rules already assigned to me. If this is not solved till September, I can provide a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions