48

Is there a way to handle http status code 422 gracefully. I am looking for the best practice here. I know that HttpStatusCode is an enum so what i tried is this,

HttpStatusCode Unprocessable = (HttpStatusCode)422;
if (Response == (HttpStatusCode)422)

but does not allow me to compare it. Am i doing something wrong here?

Whats the best possible way to add this status code at runtime.

5
  • 1
    thats what i am trying to ask....if i want to use 422 can i extend this enum?
    – golldy
    Apr 21, 2015 at 3:35
  • No you can not, why do you want extend the ResponseCode enum. That's using standard response code. stackoverflow.com/questions/757684/enum-inheritance
    – Peyman
    Apr 21, 2015 at 3:37
  • Is Respose really a HttpStatusCode? Apr 21, 2015 at 5:41
  • 1
    @codesinchaos. Let us assume it is for now. that is not the question.
    – golldy
    Apr 21, 2015 at 16:49
  • @golldy In that case I'll need to vote-to-close this as "not reproducible". Please post a working example that exhibits the problem. (HttpStatusCode)422 works perfectly well for me, at least when using the full framework. Apr 21, 2015 at 17:26

3 Answers 3

41

I was using RestSharp which returns the server response status code in a property of type HttStatusCode and I needed to check for a 422 response myself but the of course the type doesn't include it. Fortunately I was still able to test using the following:

if(response.StatusCode == (HttpStatusCode)422)
{
    // Do my stuff..
}
1
  • Cool, glad to hear this works. Makes sense, since enum is actually an int anyway.
    – Matt
    May 26, 2016 at 0:28
6

The older versions of .NET don't have this HTTP status code but some of the newer ones do (See MS Docs).
If you are using one of the older frameworks, then you can get the response and check the StatusCode like the following (like Nick said):

var httpResponseCode = Response as HttpWebResponse;
if (httpResponseCode.StatusCode == (HttpStatusCode)422)
{
    //your code here
}
1

If more than one action in your API can possibly return a 422, you could consider wrapping the check in an extension method like so:

    public static bool IsUnprocessableEntityResponse(this HttpResponseMessage message)
    {
        Requires.NotNull(message, nameof(message));

        return (int) message.StatusCode == StatusCodes.Status422UnprocessableEntity;
    }

Which you can then use in your client like so:

    if (response.IsUnprocessableEntityResponse())
        return Response.UnprocessableEntity<Resource>();

While also avoiding the 422 magic number in your source code at the same time.

2
  • 2
    StatusCodes.Status422UnprocessableEntity is part of Microsoft.AspNetCore.Http.Abstractions in case anyone reads this and wonders where it is.
    – Darrell
    Jun 11, 2019 at 11:24
  • @Darrell, nice spot. But for me, that doesn't seem to be enough justification for adding a reference to the additional assembly.
    – Chris W
    Jul 29, 2019 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.