implementing 422,423,424,507

2007-08-28 @ 23:47#

i finally decided to implement some of the extended HTTP Status Codes from RFC2518. they seem pretty valuable - esp. as i work to use HTTP as my basic protocol for programming client-server stuff over the internet. here' the codes i settled on:


10.3 422 Unprocessable Entity

   The 422 (Unprocessable Entity) status code means the server
   understands the content type of the request entity (hence a
   415(Unsupported Media Type) status code is inappropriate), and the
   syntax of the request entity is correct (thus a 400 (Bad Request)
   status code is inappropriate) but was unable to process the contained
   instructions.  For example, this error condition may occur if an XML
   request body contains well-formed (i.e., syntactically correct), but
   semantically erroneous XML instructions.

10.4 423 Locked

   The 423 (Locked) status code means the source or destination resource
   of a method is locked.

10.5 424 Failed Dependency

   The 424 (Failed Dependency) status code means that the method could
   not be performed on the resource because the requested action
   depended on another action and that action failed.  For example, if a
   command in a PROPPATCH method fails then, at minimum, the rest of the
   commands will also fail with 424 (Failed Dependency).

10.6 507 Insufficient Storage

   The 507 (Insufficient Storage) status code means the method could not
   be performed on the resource because the server is unable to store
   the representation needed to successfully complete the request.  This
   condition is considered to be temporary.  If the request which
   received this status code was the result of a user action, the
   request MUST NOT be repeated until it is requested by a separate user
   action.

the trick about all this is that .NET 2.0 HttpException class uses a predefined set of Status Codes and their associated descriptions. the extended Status Codes are missing. thus, when i set HttpException to 422, there is no description sent along. just the same number - ick. so i modidifed my error-handling with a short routine that will 'fix' the description message for these extended codes.

here's my utility to clean up the messages:

  public string FixUpHttpError(int code, string msg)
  {
      // trap for 'unsupported' error codes
      switch (code)
      {
          case 422:
              msg = "Cannot Process Entity");
              break;
          case 423:
              msg = "Locked";
              break;
          case 424:
              msg = "Failed Dependency");
              break;
          case 507:
              msg = "Insuffcient Storage";
              break;
      }

      return msg;
  }

and here's how i am using it when i throw an HttpException error in my global routine:


  Utility util = new Utility();
  HttpException hex = (HttpException)app.Context.Error;
  response.StatusCode = hex.GetHttpCode();
  response.Write(
      string.Format(
          Constants.fmt_error_module, 
          hex.GetHttpCode(), 
          util.FixUpHttpError(hex.GetHttpCode(),hex.Message))
      );

code