Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpStatusCode is any 500 type

I was wondering if there was an easier way (nicer way) to check for a 500 status code?

The only way I can think of doing this is by doing:

var statusCodes = new List<HttpStatusCode>()
{
  HttpStatusCode.BadGateway,
  HttpStatusCode.GatewayTimeout,
  HttpStatusCode.HttpVersionNotSupported,
  HttpStatusCode.InternalServerError,
  HttpStatusCode.NotImplemented,
  HttpStatusCode.ServiceUnavailable
};
if (statusCodes.Contains(response.StatusCode))
{
  throw new HttpRequestException("Blah");
}

I noticed these are the 500 types:

  • BadGateway
  • GatewayTimeout
  • HttpVersionNotSupported
  • InternalServerError
  • NotImplemented
  • ServiceUnavailable
like image 500
Dr Schizo Avatar asked Sep 26 '13 08:09

Dr Schizo


1 Answers

The Status codes starting with 5xx is a server error, so the simple method would be

if ((int)response.StatusCode>=500 && (int)response.StatusCode<600)
      throw new HttpRequestException("Server error");
like image 61
wizzardz Avatar answered Sep 21 '22 13:09

wizzardz