Domanda

I have method where it takes some parameter and the response is in json format. On success it will return int type value. But if error occurs then rest service will throw exception where as the method which is implements the REST service must return int value.

    internal static int AddCatalog(string name, Guid key, string userName)
    {
        using (var client = new HttpClient())
        {
            HttpResponseMessage response = client.PutAsync(AdminRestServiceUrl + "xml/updatecatalog?cid=" + null + "&name=" + name + "&key=" + key + "&uby=" + userName, null).Result;
            response.EnsureSuccessStatusCode();
            var cid= response.Content.ReadAsStringAsync().Result;
            return Convert.ToInt32(cid);
        }
    }

How can I handle it effieciently, if service throws error? if i do like this

if(response.IsSuccessStatusCode == HttpStatusCode.OK)
{
return convert.ToInt32(cid);
}
else
{
return ?? //how to handle error here as method must retun int type
}

Please suggest how should I pass the detailed error message to the clients in order to notify them the exact error occurred in the service. Any kind of help/suggestion is greatly appreciated.

È stato utile?

Soluzione

you can do possibly Make an "ErrorLogs" Class ,Keep "Status" Field in it , and the Error Exception in "Message" Field , if there occurs any error send status to failed

In both Cases return a Class Which has both Error Logs and and Integer Value, if there is error Return Integer as -1 and handle it on Client

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top