Generic error handler for ExtJS

When you are doing client/server communication with ExtJS you probably run into the problem that you want to handle server side errors in a generic way.
A solution that I found is to override the handleFailure function in the Ext.data.Connection class:

This handler simply is called whenever a server side failure occurs. As the Connection class handles all communication this can happen also in Ext.Ajax or Ext.data.HttpProxy. In this example the error message to be displayed is retrieved from a server generated XML with the actual text being located at XPath Reason/Text.

So a server side error message might look like this:

  • Animal

    Check the Function methods Ext gives you. You have just effectively created an interceptor. The code I would have written would be

    Ext.data.Connection.prototype.handleFailure = Ext.data.Connection.prototype.handleFailure.createInterceptor( function(response, e) {
    var errorText = Ext.DomQuery.selectValue(“Reason/Text”, response.responseXML, “Unknown Error”);
    Ext.Msg.alert(‘Error’, errorText);
    });

  • Marcus Schiesser

    Thanks Animal, that’s even better!

  • Sam

    Hello Marcus:
    It’s a very useful method for development. but does it work in ExtJS3?
    when I checked the code of Connection class, I found it will fire a “REQUESTEXCEPTION” event when request failed. so can we use event mode to handle the error?

  • Nice code.

    Ajax broadcasts 3 events that could also perform logic before any callback.

    Options is available there so you could tap into the scope to work there.

    But your code enlight me to think in some application where we could block further actions, like an aspect over the response.

    Like the chain of events in the editorgrids when validation occurs.

    cheers.

  • Hello Marcus,

    Why not handling generic server errors with the exception event in Ext.data.DataProxy? We have run into the problem that our session expired after 2 hours (CodeIgniter) and that the user clicked in her application and empty grids appeared. This is handled fine by a

    Ext.data.DataProxy.on (‘exception’, base.eventManager.onServerError);

    where base.eventManager is our eventmanager for our base functionalities.

    Kind regards,

    Johan

  • But I see that my response can be looking silly because of Ext version difference on the first article, I relate to version 3.3.

    Johan

  • Thx for the post, very useful

  • Ankit Garg

    Hi, thanks for this code, it helped me a lot. I have one question though, the error handler is fired if an ajax request is in progress and user navigates away from the page. The isAbort flag of the response object is also false in that case. Is there a way to determine that the reason for the failure is user navigating away from the page and not show the alert message in that case?