How to call .fail() from inside .done() to handle general errors?
jquery, promise
Solution
You can use a common function that's called on error in each case.
$.ajax( 'url' )
.done( function( data, textStatus, jqXHR ) {
if( data == 'Invalid' ) {
ajaxError( jqXHR, "usererror" );
}
})
.fail( ajaxError );
function ajaxError( jqXHR, textStatus, errorThrown ) {
console.log( 'Ajax error: ' + textStatus );
}
Problem
jQuery's AJAX `.fail()` is a nice error handler that handles "404 Not Found" errors and such, but how can we call it deliberately from inside `.done()` to handle general error cases, for example when data provided by user is incorrect?