Search code examples
extjssencha-touchextjs6extjs6-classic

Adding exceptions on Ext.ajax requests Ext JS 6


I want to add 400, 500 exception for all the AJAX requests. Here is what I have done for beforeload on every AJAX request.

Ext.define('Inertia.view.BaseUrl', {
singleton : true,
constructor : function(config) {

    this.initConfig(config);
    Ext.Ajax.on('beforerequest', this.onBeforeRequest, this);
},

onBeforeRequest : function(connection, options) {
    options.headers = {
        'token': 'random-token',
        'code': 'random-codeABC'
    };
}

 });

Now I want to add an exception for all the requests, that if the request somehow didn't load I want to catch an exception. How would I do that I try this but it didn't work

Ext.define('Inertia.view.BaseUrl', {
singleton : true,
constructor : function(config) {

    this.initConfig(config);
    Ext.Ajax.on('beforerequest', this.onBeforeRequest, this);
    Ext.Ajax.on('exception', this.exception, this);
},

onBeforeRequest : function(connection, options) {
    options.headers = {
        'token': 'random-token',
        'code': 'random-codeABC'
    };
},
 exception: function (){
     alert('An Exception occured...!');
   }
 });

Solution

  • You can use it like below:-

    /**
     * @ajaxrequest error handling
     */
    Ext.Ajax.on('requestexception', function (conn, response, options, eOpts) {
        try {
            alert(response.status);
        } catch (error) {
            console.log(error);
        }
    });
    

    Or like this:-

    requestexception: function (conn, response, options, eOpts) {
        try {
            alert(response.status);
        } catch (error) {
            console.log(error);
        }
    }