This is probably a stupid question and I'm just missing something obvious. I've looked around at handling web errors and alike and I just can't seem to find an answer as to why this is happening.
Basically the server that I'm calling from sends json and back but has intermittent issues on requests - Namely a 502 BadGateway - no others. When I say intermittent I stress that, as it could work perfectly for 200 requests without fail over 5 hours then throw a 502, but it could also happen on the first request. Hard to predict. Also, it should be noted that I have no access to the server, it's configs or anything like that. Completely remote.
In dev environment I can continue through the exception and the program continues to run and - to same server using the same request - on next request, should I not receive the 502 it processes successfully. However, standalone away from the dev platform I get a hard crash with no exception data.
I've tried moving the try/catch around, separating the problem call from the rest of the code, combining it so on so forth and I've always received the same issue.
It is late and I'm probably just not handling it correctly so any guidance is appreciated. I'm refusing to give up on this tonight - been a thorn in my side for about a week now. If I'm just a scrub and would've seen in on fresh eyes, laugh at me and call me out.
Thanks in advance
ps. exception is being issued at getResponse()
try
{
using (HttpWebResponse inboundResponse = (HttpWebResponse)outboundRequest.GetResponse()) //exception here for 502
using (StreamReader stream = new StreamReader(inboundResponse.GetResponseStream()))
using (JsonTextReader reader = new JsonTextReader(stream))
{
List<T> outputData = new List<T>();
while (reader.Read())
{
JsonSerializer serializer = new JsonSerializer();
var tempData = serializer.Deserialize<T>(reader);
outputData.Add(tempData);
}
inboundResponse.Close();
return outputData;
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
UIController.addSystemMessageToChat("Protocol Error");
switch(((HttpWebResponse)e.Response).StatusCode)
{
case HttpStatusCode.BadGateway:
UIController.addSystemMessageToChat("Resending Response");
//process exception
default:
throw;
}
}
}
It may not be a WebException
Try adding a catch for Exception
into the handler:
try
{
using (HttpWebResponse inboundResponse = (HttpWebResponse)outboundRequest.GetResponse()) //exception here for 502
using (StreamReader stream = new StreamReader(inboundResponse.GetResponseStream()))
using (JsonTextReader reader = new JsonTextReader(stream))
{
List<T> outputData = new List<T>();
while (reader.Read())
{
JsonSerializer serializer = new JsonSerializer();
var tempData = serializer.Deserialize<T>(reader);
outputData.Add(tempData);
}
inboundResponse.Close();
return outputData;
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
UIController.addSystemMessageToChat("Protocol Error");
switch(((HttpWebResponse)e.Response).StatusCode)
{
case HttpStatusCode.BadGateway:
UIController.addSystemMessageToChat("Resending Response");
//process exception
default:
throw;
}
}
}
catch (Exception e)
{
// catch and handle an unknown / unexpected exception type
}