SonarQube在出现异常时显示错误

时间:2019-06-10 05:59:23

标签: java sonarqube

对于以下代码,

Sonarqube显示错误,并显示“要么记录日志,要么抛出该异常”。在catch块中,我们该如何处理

  private ResponseEntity<String> getResponse(String url,
                                                      String logName,
                                                      HttpMethod httpMethod,
                                                      HttpEntity<String> httpEntity,
                                                      HttpServletRequest httpServletRequest)
    {
        httpServletRequest.setAttribute("api", logName);
        ResponseEntity<String> checkEntity;
        try {
            if(logName.equals("Activate All Offer Api")){
                checkEntity = requestFactory.getRestTemplate().exchange(url, httpMethod, httpEntity, String.class);
            }else {
                checkEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
            }
        } catch (Exception e) {
        throw new LocalHttpClientErrorException(e.getLocalizedMessage());
    }
        return checkEntity;
    }

1 个答案:

答案 0 :(得分:0)

要消除警告,您需要使用initCause(Throwable)方法将原始异常作为原因附加到LocalHttpClientErrorException,无论是在构造函数参数中,还是在不可能的情况下。但是那里也有其他一些问题。

  1. 您正在捕获通用Exception。这始终是严重的代码异味的迹象。
  2. 您要将本地化的消息传递给LocalHttpClientErrorException。本地化应该是UI层的责任,而不是业务逻辑。
相关问题