Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I understand that the API could be returning status codes like this:
200 - OK
400 - Bad Request
500 - Internal Server Error

Here is my code, how to handle the error from API in my controller?

C#
public ActionResult Get()
       {
       string token_access = User.Identity.GetAccessToken();
               Uri apiHistoryUri = new Uri(new Uri(ConfigurationManager.AppSettings["ida:ApiUri"]), "api/student/name");
               var client = new RestClient(apiHistoryUri);

               var request = new RestRequest(Method.GET);
               request.AddHeader("content-type", "application/x-www-form-urlencoded");
               request.AddHeader("cache-control", "no-cache");
               request.AddHeader("authorization", token_access);

               IRestResponse response = client.Execute(request);
               var content = response.Content;
               return View(content);
      }



The response from API can be anything, how to handle the exception here.
C#
IRestResponse response = client.Execute(request);
var content = response.Content;


What I have tried:

I tried to do this way, but im not sure.

IRestResponse response = client.Execute(request);
try
{
var content = response.Content;
return View(content);
}
catch {
throw (new Exception(res.StatusDescription.ToString()));
}
Posted
Updated 13-Sep-21 9:42am
Comments
njammy 17-Aug-16 6:13am    
Why not just do
var statusCode = response.StatusCode;
and then handle it in a generic handler to build the appropriate response back to the client.
wa.war 18-Aug-16 0:08am    
try{
//some code here

IRestResponse response= clt.Execute(req);
if (res.StatusCode.ToString().Equals("OK"))
{
var id = (string)JsonConvert.DeserializeObject(response.Content);
return Content("Fax Sent Successfully! - " + id);
}
else
{
throw (new HttpException(response.StatusDescription.ToString()));
}
catch (Exception ex)
{
return Content("Fail to send, please try again.");
}

** is this code acceptable?
Richard MacCutchan 17-Aug-16 6:38am    
AFAIK those are status codes returned in the message, not exceptions. So you need to extract them from the response and check for the ones you can handle. Any that your code cannot deal with you just need to report to the user.
wa.war 17-Aug-16 22:45pm    
I understand what do you mean, thanks. but the statuscode can be anything right? Not just 3 status that im mentions above. What is the best way to handle it and report back to user? can you give me an example?
Richard MacCutchan 18-Aug-16 3:21am    
You only need to check for good status, and the ones that you can recover from in your code. For all the others you just post the full status message to the user and let them figure it out.

1 solution

C#
IRestResponse response = client.Execute(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
	
}
else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
	
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
	
}
else
{
	throw new Exception(string.Format("No se puede consumir el API: {0}", response.ErrorException));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900