简体   繁体   English

在我的Java代码中,关键的SonarLint问题S1166是否是误报?

[英]Is critical SonarLint issue S1166 in my Java code a false positive or not?

SonarLint 1.0.0 for Eclipse flags a critical issue in my code and I can't see why and how to fix it. SonarLint 1.0.0 for Eclipse在我的代码中标记了一个关键问题,我无法理解为什么以及如何解决它。 It really looks like a false positive to me—or am I missing something? 这对我来说真的是假阳性 - 或者我错过了什么?

import org.apache.log4j.Logger;

[...]

public final class Foo {

    private static final Logger logger = Logger.getLogger(Foo.class);

    [...]

    public static void foo() {

        MyCommand command = new MyCommand(foo, bar);
        try {
            commandService.executeCommand(command);
        } catch (CommandException e) {
            logger.error("My command execution failed", e);
        }
    }

    [...]

Here's an excerpt of the matching SonarLint rule description : 以下是匹配的SonarLint规则说明的摘录:

When handling a caught exception, the original exception's message and stack trace should be logged or passed forward. 处理捕获的异常时,应记录或传递原始异常的消息和堆栈跟踪。

Noncompliant Code Example 不合规的代码示例

\n// Noncompliant - exception is lost //不合规 - 异常丢失\ntry { /* ... */ } catch (Exception e) { LOGGER.info("context"); try {/ * ... * /} catch(Exception e){LOGGER.info(“context”); } }   \n\n// Noncompliant - exception is lost (only message is preserved) //不合规 - 异常丢失(仅保留消息)       \ntry { /* ... */ } catch (Exception e) { LOGGER.info(e.getMessage()); try {/ * ... * /} catch(Exception e){LOGGER.info(e.getMessage()); } }\n\n// Noncompliant - exception is lost //不合规 - 异常丢失\ntry { /* ... */ } catch (Exception e) { throw new RuntimeException("context"); try {/ * ... * /} catch(Exception e){throw new RuntimeException(“context”); } }\n

Compliant Solution 合规解决方案

\ntry { /* ... */ } catch (Exception e) { LOGGER.info(e); try {/ * ... * /} catch(例外e){LOGGER.info(e); } }   \n\ntry { /* ... */ } catch (Exception e) { throw new RuntimeException(e); try {/ * ... * /} catch(Exception e){throw new RuntimeException(e); } }\n\ntry { /* ... */ } catch (RuntimeException e) { try {/ * ... * /} catch(RuntimeException e){\n    doSomething(); 做一点事();  \n    throw e; 扔掉;\n} catch (Exception e) { } catch(例外e){\n    // Conversion into unchecked exception is also allowed //也允许转换为未经检查的异常\n    throw new RuntimeException(e); 抛出新的RuntimeException(e);\n} }\n

In my opinion, my code qualifies for the first variant of the given compliant solutions, but SonarLint does not accept it. 在我看来,我的代码符合给定的兼容解决方案的第一个变体,但SonarLint不接受它。

There was another discussion of Sonar rule S1166 a little while ago, but it's not really about the same issue I have. 不久之前还有另一个关于Sonar规则S1166讨论 ,但它并没有真正与我有同样的问题。

Edit: In response to question below: I use log4j for logging. 编辑:回答以下问题:我使用log4j进行日志记录。 I expanded the code to reflect this. 我扩展了代码以反映这一点。

You are, in fact, logging the original exception's message and stack trace; 实际上,您正在记录原始异常的消息和堆栈跟踪; this is an erroneous finding. 这是一个错误的发现。

It may be that the rule doesn't have specific knowledge of Log4j, but lacking omniscience of all logging libraries, the fact that the exception is passed as a parameter could suffice. 可能是规则没有Log4j的特定知识,但缺乏所有日志库的全知性,异常作为参数传递的事实就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM