Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call .fail() from inside .done() to handle general errors?

Tags:

jquery

promise

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?

like image 342
Ante Vrli Avatar asked Mar 10 '13 08:03

Ante Vrli


People also ask

How to handle errors in JavaScript?

JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs.

How do you handle errors in Ajax call?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

Does JavaScript have exceptions?

In JavaScript, all exceptions are simply objects. While the majority of exceptions are implementations of the global Error class, any old object can be thrown. With this in mind, there are two ways to throw an exception: directly via an Error object, and through a custom object.

How do you return an error in JavaScript?

To return errors from functions as promises, there are generally two ways to produce rejected promises: The reject function of your Promise library. Throwing an exception in a then callback or returning a rejected promise from it will reject the resulting promise.


2 Answers

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 );
}
like image 130
JJJ Avatar answered Oct 27 '22 09:10

JJJ


@Juhana's code will be good for most eventualities.

If, however, errors might arise that cannot be reliably predicted by inspection of data alone, then consider this variant :

$.ajax( 'url' ).done( function( data, textStatus, jqXHR ) {
    try {
        var dataLooksGood = ......;//(boolean) Test data for predictable data errors here
        if( !dataLooksGood ) throw( new Error("data invalid"); )
        //Handle data here.
        //Any naturally-ocurring errors or further manually-thrown errors will be caught below.
    }
    catch(err) {
        ajaxError( jqXHR, "AJAX successful but: " + err.message );
    }
}).fail( ajaxError ).always(function() {
    //Here do whatever is necessary after success or error
});


function ajaxError( jqXHR, textStatus, errorThrown ) {
    console.log( 'AJAX error: ' + textStatus );
}

By catching errors, the always handler will be allowed to fire even if an unpredicted error occcurs. Thus you could better guarantee that a "loading..." message or spinner graphic was killed regardless of an error having occurred.

like image 27
Beetroot-Beetroot Avatar answered Oct 27 '22 10:10

Beetroot-Beetroot