title: Best Practice : Ways to handle errors and exception in web api controllers?
body: <p>I am working on a project and have relied heavily on web api for all my client side operations, be it account details update, new details added, modify everything has been done with ASP.NET Web Api and Backbone.js</p>
  
  <p><strong>Current Scene :</strong></p>
  
  <p>In the current scheme of things, I am returning a boolean value from my web api controllers, to indicate whether the operation was successfull or not.</p>
  
  <p>Example : </p>
  
  <pre><code>[ActionName("UpdateAccountDetails")]
  public bool PostAccountDetails(SomeModel model)
  {
      bool updateStatus = _customService.UpdateAccountDetails(model);
      return updateStatus;
  }
  </code></pre>
  
  <p>so after making an ajax call to this action, I check the response for true/false and display error or success messages.</p>
  
  <p><strong>Problem :</strong></p>
  
  <p>Now what happened was I started getting exceptions in my action, and the action kept returning false, and the error message was shown. But I was not able to find why ?</p>
  
  <p>So I was wondering if there is a standard api response structure which every one follows ?</p>
  
  <p>I had initially come up with this idea to have each web api action to return this class </p>
  
  <pre><code>public class OperationStatus
  {
      public bool Result { get; set; } // true/false
      public string Status { get; set; } // success/failure/warning
      public List&lt;string&gt; WarningMessages { get; set; }
      public List&lt;string&gt; ErrorMessages { get; set; }
      public string OtherDetails { get; set; }
  }
  </code></pre>
  
  <p>This change would be a major change and would be time and resource consuming, so I thought its better to have a second/third/fourth opinion on this.</p>
  
  <p>Please put some thoughts on this.</p>
  
  <p><strong>Update :</strong></p>
  
  <p>With some <a href="https://stackoverflow.com/a/13951599/1182982">little help</a> from <a href="https://stackoverflow.com/users/1440706/mark-jones">Mark Jones</a>, I have come up with this</p>
  
  <pre><code>[ActionName("UpdateAccountDetails")]
  public HttpResponseMessage PostAccountDetails(SomeModel model)
  {
      bool updateStatus;
      string errorMessage;
      try{
          updateStatus = _customService.UpdateAccountDetails(model);
          if(updateStatus)
          {
              return Request.CreateResponse(HttpStatusCode.OK);
          }
          return Request.CreateResponse(HttpStatusCode.InternalServerError);
      }
      catch(Exception exception)
      {
          errorMessage = exception.Message;
          return Request.CreateResponse(HttpStatusCode.InternalServerError, errorMessage);
      }
  
      return updateStatus;
  }
  </code></pre>
  
  <p>Any thought on this ?</p>