首页 > 解决方案 > 存在异常时 SonarQube 显示错误

问题描述

对于下面的代码 Sonarqube 显示错误,说“要么记录或重新抛出这个异常”。fof 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;
    }

标签: javasonarqube

解决方案


要使警告消失,您需要在构造函数参数中将原始异常作为原因附加到 LocalHttpClientErrorException ,或者如果这不可能,请使用 initCause(Throwable) 方法。但那里也存在一些其他问题。

  1. 您正在捕获通用异常。这始终是严重代码异味的标志。
  2. 您正在将本地化消息传递给 LocalHttpClientErrorException。本地化应该是 UI 层的职责,而不是业务逻辑。

推荐阅读