Dr Schizo
Dr Schizo

Reputation: 4386

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:

Upvotes: 15

Views: 12050

Answers (1)

wizzardz
wizzardz

Reputation: 5874

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");

Upvotes: 15

Related Questions