My code handles some rest APIs (GET methods). This GET method, returns a response like this:
{"error":0,"Logs":[{"LoggerIdx":"91","OfficeID":"MIA1A0955","Agent":"581A78AD"}]}
and if the query doesn't find anything, returns:
{"error":0,"Logs":[{"No values found"}]}
The code I'm using call this API to retrieve the values and show a report is:
private string uri = "http://localhost";
public async Task<List<T>> GetWSObjects<T>(string uriActionString)
{
return new List<T> { await this.GetWSObject<T>(uriActionString) };
}
public async Task<T> GetWSObject<T>(string uriActionString)
{
T returnValue =
default(T);
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(uriActionString);
response.EnsureSuccessStatusCode();
returnValue = JsonConvert.DeserializeObject<T>(((HttpResponseMessage)response).Content.ReadAsStringAsync().Result);
}
return returnValue;
}
catch (Exception e)
{
throw (e);
}
}
returnValue tries to fill my model with the values in the response. However, when the response only contains "No values found" it breaks(obviously). My question is, should I place a try-catch within this try-catch to handle this behavior? The problem is that the whole exception is being shown to the user, not only "Not values found". Suggestions? My model is:
public class BuildingReportModel
{
public string message1 { get; set; }
public Log[] Logs { get; set; }
}
public class Log
{
public string ProdLoggerIdx { get; set; }
public string OfficeID { get; set; }
public string Agent { get; set; }
}