Suppose I have web api application using ASP.NET. In one of the actions I'm checking if I can activate a free trial period for the given user. Now from one point of view, result of this action is either successful or unsuccessful. But in case of an unsuccessful result, I'd like to inform the client why the request has been denied and then the client can decide what to do.
There are multiple if-checks in this action and each one of them can deny the incoming request. Further more, we can have different successful result conditions as well.
What is the right way to return these different conditions so the client can easily distinguish between them?
What I'm doing right now is something like this:
public IHttpActionResult ActivateFreeTrial()
{
BaseResult result = new BaseResult(); // This class has a property called Status
if(!FirstCondition)
{
// Unsuccessful
result.Status = 90;
return Ok(result)
}
if(!SecondCondition)
{
// Unsuccessful
result.Status = 91;
return Ok(result)
}
// Some more checks...
if(!SixthCondition)
{
// Successful
result.Status = 1;
return Ok(result);
}
else {
// Successful
result.Status = 2;
return Ok(result);
}
}
Client needs to know which condition generated the result so that's why I'm assigning a number to distinguish between the different conditions. So in client-side I can do something like:
CallActivateFreeTrial()
.then(function(res){
if (res.Status == 1)
{
// Successful result but with status 1
}
else if (res.Status == 2)
{
// Successful result but with status 2
}
else if (res.Status == 90)
{
// Unsuccessful result but with status 90
}
else if (res.Status == 91)
{
// Unsuccessful result but with status 91
}
}, function(error){
})
I hardly think what I'm doing right now is considered to be a good practice. Is there a better way to return different conditional results to the client?