【问题标题】:How to fix Sonar Violation: Invoke methods only conditionally and Either Log or rethrow the exception如何修复声纳违规:仅有条件地调用方法并记录或重新抛出异常
【发布时间】:2020-02-15 09:27:52
【问题描述】:

我的代码中有以下语句违反了某些声纳规则。

LOG.info("Fetched: {}", mapper.writeValueAsString(requests));

这个为mapper.writeValueAsString(requests)显示Invoke method(s) only conditionally

}catch (Exception e) {
            LOG.error("Exception occurred while trying to fetch requests", e);
            throw new GenericException(Error.FETCH_REQUEST_ERR_001.name(), e.getMessage(), Error.FETCH_REQUEST_ERR_001.value());
        }

这个显示Either log this exception and handle it, or rethrow it with some contextual information.违反规则。

关于如何解决这些问题的任何想法。感谢任何帮助。

【问题讨论】:

    标签: sonarqube


    【解决方案1】:

    我使用 IntelliJ,并且在与您的情况类似的情况下,SonarLint 插件为我接受了以下修复:

    if(LOG.isInfoEnabled() && mapper != null){
     LOG.info("Fetched: {}", mapper.writeValueAsString(requests)); // Your code
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 2022-01-12
      • 2015-03-23
      • 2015-11-18
      • 2012-09-04
      • 1970-01-01
      • 2016-11-13
      • 2016-05-30
      相关资源
      最近更新 更多