title: Can I return custom error from JsonResult to jQuery ajax error method?
body: <p>How can I pass custom error information from an ASP.NET MVC3 <code>JsonResult</code> method to the <code>error</code> (or <code>success</code> or <code>complete</code>, if need be) function of <code>jQuery.ajax()</code>? Ideally I'd like to be able to:</p>
  
  <ul>
  <li>Still throw the error on the server (this is used for logging)</li>
  <li>Retrieve custom information about the error on the client</li>
  </ul>
  
  <p>Here is a basic version of my code:</p>
  
  <h3>Controller JsonResult method</h3>
  
  <pre><code>public JsonResult DoStuff(string argString)
  {
      string errorInfo = "";
  
      try
      {
          DoOtherStuff(argString);
      }
      catch(Exception e)
      {
          errorInfo = "Failed to call DoOtherStuff()";
          //Edit HTTP Response here to include 'errorInfo' ?
          throw e;
      }
  
      return Json(true);
  }
  </code></pre>
  
  <h3>JavaScript</h3>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "../MyController/DoStuff",
      data: {argString: "arg string"},
      dataType: "json",
      traditional: true,
      success: function(data, statusCode, xhr){
          if (data === true)
              //Success handling
          else
              //Error handling here? But error still needs to be thrown on server...
      },
      error: function(xhr, errorType, exception) {
          //Here 'exception' is 'Internal Server Error'
          //Haven't had luck editing the Response on the server to pass something here
      }
  });
  </code></pre>
  
  <p>Things I've tried (that didn't work out):</p>
  
  <ul>
  <li>Returning error info from <code>catch</code> block
  <ul>
  <li>This works, but the exception can't be thrown</li>
  </ul></li>
  <li>Editing HTTP response in <code>catch</code> block
  <ul>
  <li>Then inspected <code>xhr</code> in the jQuery error handler</li>
  <li><code>xhr.getResponseHeader()</code>, etc. contained the default ASP.NET error page, but none of my information</li>
  <li>I think this may be possible, but I just did it wrong?</li>
  </ul></li>
  </ul>