简体   繁体   English

获取 Java 代码的声纳问题“返回值包含操作状态代码时不应被忽略”

[英]Sonar Issue "Return values should not be ignored when they contain the operation status code" getting for Java code

I am getting the Sonar Issue "Return values should not be ignored when they contain the operation status code" for below line of code.我收到以下代码行的声纳问题“返回值包含操作状态代码时不应被忽略”。

 directory.delete();

Do something with the "boolean" value returned by "delete()" error I am getting.对我得到的“delete()”错误返回的“布尔”值做一些事情。 I tried to add condition like我试图添加条件

if(!directory.delete()){
            logger.error("failed to delete");
        }

Sonar issue is getting fixed but it is affecting for code, please let me know, how to fix the issue.声纳问题正在修复,但它会影响代码,请告诉我如何解决该问题。

In this case you can use logger without side-effect for the code, for instance like that:在这种情况下,您可以对代码使用没有副作用的记录器,例如:

var retVal = directory.delete();
logger.info("entity " + reVal + " was deleted");

Sonar is telling you might better do something when the directory cannot be deleted. Sonar 告诉您,当目录无法删除时,您最好做点什么。 So leave the warning if you cannot think of some solution.因此,如果您想不出一些解决方案,请留下警告。

Or log, but on trace level, which normally will not be logged, as opposed to error level logging.或记录,但在跟踪级别,通常不会记录,与错误级别记录相反。

if (!directory.delete()) {
    logger.trace("failed to delete: " + directory);
}

An other solution: I would also advise to use the methods of Files anyway.另一个解决方案:我也建议使用Files的方法。 There are some improvements.有一些改进。 Also in this case:同样在这种情况下:

Path dir = directory.toPath();
try {
    Files.delete(dir); // void, instead exceptions
    if (Files.deleteIfExists(dir)) { // boolean: whether
                                     // this call deleted the directory.
       ++deleteCount;
    }
} catch (NoSuchFileException | DirectoryNotEmptyException
        | IOException | SecurityException e {
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 java.io.IOException:服务器返回 HTTP 响应代码:URL 的 400:使用返回 400 状态代码的 url 时 - Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code 哪个HTTP代码应返回为状态? - Which HTTP code should return as status? 当我运行此代码时,它会在 java 中返回“退出状态 143” - When I run this code it return that “exit status 143” in java 从 Java 使用 LocalStack 时获取 AmazonKinesisException 状态代码:502 - Getting an AmazonKinesisException Status Code: 502 when using LocalStack from Java 如何使用Sonar WS Client和Java代码获取问题列表? - How to get issue list using Sonar WS Client with Java code? 声纳对webapp进行分析时,是否只是在分析Java代码? - When sonar performs an analysis of a webapp is it just analysing java code? SONAR问题“主要”不应该“扔”任何东西JAVA 7 - SONAR issue “main” should not “throw” anything JAVA 7 没有创建资源时,我应该为POST返回什么HTTP状态代码? - What HTTP status code should I return for POST when no resource is created? 上载档案时发生错误时,应该返回什么HTTP状态码 - what http status code should be return when we get error while uploading file 恶意代码漏洞-字段应受程序包保护。 如何解决这个声纳问题? - Malicious code vulnerability - Field should be package protected. How to fix this sonar issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM