title: What is the best way to return errors from a WCF service in a RESTful way?
body: <p>Using WCF in a RESTful way seems great. I’m a big fan of the big guns like simplicity and flexibility, but I also love the way the Urls end up looking. What can I say, I’m a programmer.</p>
  <p>The simple API for retrieving and editing resources is matched by an almost equally simple set of possible error responses, and I can’t help feeling that to keep to a “pure” RESTful approach I may be cutting my nose off to spite by face, or more specifically, the nose of my web service consumers. I could be wrong, but there doesn’t seem to be very many Http error codes that I can use, and no ways to pass back a custom error message.</p>
  <p>To clarify, I am talking about proper exceptional errors and not expected errors. I want to actually communicate a problem to the user to help them identify what they need to do to correct it.</p>
  <p>Possible options I am considering...</p>
  <ol>
  <li><p><strong>Just use the Http error codes</strong> – This seem like it would be too restrictive in what I am able to express, and won’t allow me to supply a custom message. Please(!) correct me if I am wrong.</p>
  </li>
  <li><p><strong>Always return Http Success but return custom error objects</strong> – Obviously the most flexible but certainly not the most RESTful.</p>
  </li>
  </ol>
  <p>I would really appreciate it if anyone could share some real world experience of this particular problem.</p>
  <hr />
  <h3>Update</h3>
  <p>Thanks for the suggestion of using the <code>StatusDescription</code> property of the <code>OutgoingWebResponseContext</code> object. It seemed at first to be something that I could use.</p>
  <p>I have come to the conclusion that my second option above is not for me. I want to stick to the what Http can give me.</p>
  <p>I am having problems getting it to work, however. Regardless of the value I supply for this property, it doesn’t get returned in the response.</p>
  <p>My service method looks like this</p>
  <pre><code>public MyType GetMyTypes(string criteria)
  {
      try
      {
          return GetMyTypes();
      }
      catch (Exception ex)
      {
          OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
          response.StatusCode = HttpStatusCode.Forbidden;
          response.StatusDescription = &quot;A Big fat error occurred&quot;;
          return null;
      }
  }
  </code></pre>
  <p>And here is the raw response message. There is no mention of the custom message...</p>
  <blockquote>
  <p>HTTP/1.1 403 Forbidden<br />
  Server: ASP.NET Development Server/9.0.0.0<br />
  Date: Wed, 07 Jan 2009 14:01:20 GMT<br />
  X-AspNet-Version: 2.0.50727<br />
  Cache-Control: private<br />
  Content-Length: 0<br />
  Connection: Close</p>
  </blockquote>
  <p>It's not as if I just need to access the correct property on the client. The information is simply not being sent across the link.</p>
  <p>What does this <code>StatusDescription</code> property actually do?</p>
  <hr />
  <h3>Update</h3>
  <p>I never did find out how to set the StatusDescription property. I ended up not including any error message at all, and going solely with the Http status codes. I have chosen to expose both Soap and Restful endpoints for my services, and so clients can choose which they prefer to use – the simple Restful messages or the relatively richer Soap messages.</p>