'Can this code be pulled out into a separate method?
Can the whole try-catch block shown below be pulled out into a method?
My boss asked me to do it but I feel like I'm missing something because I don't think it can be pulled out (without too much complexity, because of the return Ok(response); line in it). I don't want to say no to him without being sure.
[Authorize]
[HttpPost("endpoint123")]
public async Task<ActionResult> Endpoint123(Data data)
{
ResponseObject response = new ResponseObject
{
Success = false,
ErrorMessage = "",
UserAdded = false
};
try
{
var e = await Request111(data); // this can throw exception
if (e.result != -1)
{
var success = await Request222(data); // this can throw exception
if (!success)
{
response.ErrorMessage += "errorMessage123";
// _loggingService is a global variable
_loggingService.Log(LogLevel.Error, Priority.High, "Error happened");
return Ok(response); // <-- Notice this returns ok response
}
}
response.Success = true;
}
catch (Exception ex)
{
response.ErrorMessage += ex.Message;
_loggingService.Log(LogLevel.Error, Priority.High, "Error happened");
response.Success = false;
}
response.UserAdded = true;
return Ok(response);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
