java - 存在异常时 SonarQube 显示错误

标签 java sonarqube

对于下面的代码,Sonarqube 显示错误,提示“要么记录或重新抛出此异常。”对于 catch block ,我们如何处理相同的问题

  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;
    }

最佳答案

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

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

关于java - 存在异常时 SonarQube 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521188/

相关文章:

java - 通过 Retrofit 登录和注册

java - 是否可以将对象从对象重新键入为整数

java - JPanel 中的 GridBagLayout,组件不改变 x || y 位置

java - 在 Java 类中访问 BIRT 数据集的数据

java - 如何修复 Pattern.compile(regex, Pattern.CASE_INSENSITIVE) 上的 Sonar 关键问题;

Java:带通知的 List<E> 装饰器实现

android - 如何包含 Sonar 的 Android 库和类?

android - 检查android代码质量的替代方法

sonarqube - 用于跳过对类的 Sonar 分析的注释

sonarqube - 为 SonarQube 生成虚拟数据