对于下面的代码,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) 方法。但也存在一些其他问题。
- 您正在捕获通用异常。这始终是严重代码异味的标志。
- 您正在将本地化消息传递给 LocalHttpClientErrorException。本地化应该是 UI 层的责任,而不是业务逻辑。
关于java - 存在异常时 SonarQube 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521188/