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.
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!
CodePudding user response:
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 if
s. Or you could say result?.status
instead (null-safe).