title: jQuery AJAX Error Handling (HTTP Status Codes)
body: <p>We have an API which uses proper HTTP status codes for errors, and responds with JSON-encoded responses and appropriate <code>Content-Type</code> header.  My situation is that <code>jQuery.ajax()</code> triggers the <code>error</code> callback when it encounters an HTTP error status, and not the <code>success</code> callback, so even when we have an intelligible JSON response, we have to resort to something like this:</p>
  
  <pre><code>$.ajax({
      // ...
      success: function(response) {
          if (response.success) {
              console.log('Success!');
              console.log(response.data);
          } else {
              console.log('Failure!');
              console.log(response.error);
          }
      },
      error: function(xhr, status, text) {
          var response = $.parseJSON(xhr.responseText);
  
          console.log('Failure!');
  
          if (response) {
              console.log(response.error);
          } else {
              // This would mean an invalid response from the server - maybe the site went down or whatever...
          }
      }
  });
  </code></pre>
  
  <p>Is there a better paradigm than doing identical error handling in two spots in each <code>jQuery.ajax()</code> call?  It's not very DRY, and I'm sure I've just missed something somewhere on good error handling practices in these cases.</p>