46

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.

Peyman
  • 3,068
  • 1
  • 18
  • 32
golldy
  • 1,279
  • 1
  • 15
  • 31
  • Just check this [link](https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx), there is no 422 status code – Peyman Apr 21 '15 at 03:33
  • 1
    thats what i am trying to ask....if i want to use 422 can i extend this enum? – golldy Apr 21 '15 at 03:35
  • No you can not, why do you want extend the ResponseCode enum. That's using standard response code. http://stackoverflow.com/questions/757684/enum-inheritance – Peyman Apr 21 '15 at 03:37
  • Is `Respose` really a `HttpStatusCode`? – CodesInChaos Apr 21 '15 at 05:41
  • 1
    @codesinchaos. Let us assume it is for now. that is not the question. – golldy Apr 21 '15 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. – CodesInChaos Apr 21 '15 at 17:26

3 Answers3

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..
}
Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
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
}
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
user1678668
  • 61
  • 1
  • 3
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.

aevitas
  • 3,753
  • 2
  • 28
  • 39
  • 2
    StatusCodes.Status422UnprocessableEntity is part of Microsoft.AspNetCore.Http.Abstractions in case anyone reads this and wonders where it is. – Darrell Jun 11 '19 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 '19 at 12:03