Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't determine the exact sonarqube issue here

I have following API end point. this is the part of my code.

[HttpPost]
[Route("alert/SMS")]
public async Task<IActionResult> SendSMS(SendSMSReq req)
{
    try
    {
        CHeader cHeader = new CHeader
        {
            uuid = Request.Headers["uuid"],
            channel = Request.Headers["channel"]
        };

        if (string.IsNullOrEmpty(req.mobileNo) || string.IsNullOrEmpty(req.message))
        {
            APILog.LogInformation(System.Reflection.MethodBase.GetCurrentMethod().Name, cHeader.uuid, "Mobile no or Null request received");
            var errorMsg001 = await Translation.Translation.GetRedisCache(cHeader.uuid, "core.alert.001", cHeader.channel);
            string message = errorMsg001.messageCode != "001" && errorMsg001.messageCode != "999999" ? errorMsg001.messageText : "Invalid Mobile Number or Message";
            return BadRequest(message);
        }

        var result = await sendSMSRepository.SendSMS(req, cHeader);
        
        if(result.status == "001")
        {
            var errorMsg001 = await Translation.Translation.GetRedisCache(cHeader.uuid, "core.alert.001", cHeader.channel);
            result.message = errorMsg001.messageCode != "001" && errorMsg001.messageCode != "999999" ? errorMsg001.messageText : "Invalid Mobile Number or Message";
            return BadRequest(result);
        }
        if (result == null)
        {
            APILog.LogInformation(System.Reflection.MethodBase.GetCurrentMethod().Name, cHeader.uuid, "Mobile no or Null request received");
            var errorMsg001 = await Translation.Translation.GetRedisCache(cHeader.uuid, "core.alert.002", cHeader.channel);
            result.message = errorMsg001.messageCode != "001" && errorMsg001.messageCode != "999999" ? errorMsg001.messageText : "Error occurred, please try again after few minutes";
            return NotFound("Error occurred, please try again after few minutes");
        }

        return Accepted(result);
    }
    catch (Exception ex)
    {
        APILog.LogError(System.Reflection.MethodBase.GetCurrentMethod()?.Name, req?.mobileNo, ex, "Exception");
        
        return NotFound("Error occurred, please try again after few minutes");
    }
   
}

In the above code, Sonarqube showing major bus in this line.

    if (result == null)
    {
        APILog.LogInformation(System.Reflection.MethodBase.GetCurrentMethod().Name, cHeader.uuid, "Mobile no or Null request received");
        var errorMsg001 = await Translation.Translation.GetRedisCache(cHeader.uuid, "core.alert.002", cHeader.channel);
        result.message = errorMsg001.messageCode != "001" && errorMsg001.messageCode != "999999" ? errorMsg001.messageText : "Error occurred, please try again after few minutes";
        return NotFound("Error occurred, please try again after few minutes");
    }

Is shows, following error message.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Change this condition so that it does not always evaluate to 'false'; some subsequent code is never executed.

What’s wrong here, someone please explain me to resolve this error. Thanks!

>Solution :

        if(result.status == "001")
        {
            // ...
        }
        if (result == null)
        {
            // ...
        }

I guess if result was really null, your code would blow up already at result.status, and hence would never reach the last if.

You could change the order of the ifs. Or you could say result?.status instead (null-safe).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading