java - 存在异常时 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;
}
解决方案
要使警告消失,您需要在构造函数参数中将原始异常作为原因附加到 LocalHttpClientErrorException ,或者如果这不可能,请使用 initCause(Throwable) 方法。但那里也存在一些其他问题。
- 您正在捕获通用异常。这始终是严重代码异味的标志。
- 您正在将本地化消息传递给 LocalHttpClientErrorException。本地化应该是 UI 层的职责,而不是业务逻辑。
推荐阅读
- android-studio - Error related to resources_ap after upgrading to Android Studio 3.4
- google-apps-script - 如何让用户使用脚本(绑定到 Gsheet)来更新另一个 Gsheet 中的单元格(用户没有编辑权限)?
- wpf - 在桌面桥下运行时无法访问输出目录中的文件
- python - 根据来自 Python/Flask 的数据渲染 React 组件
- r - 在边缘列表R列表中匹配顶点属性
- python - 有没有办法让方法网格在框架内?
- python-3.x - str 对象没有属性“get”
- chilkat - Chilkat 在不知道类型的情况下加载 SSHKey
- kubernetes - 重新启动 Pod 时发出警报:Windows 容器上的 Prometheus
- visual-studio-code - Visual Studio Code won't open on Ubuntu 16.04 via VNC