patryks
patryks

Reputation: 692

extjs4 global network exception listener

I want to write a listener that will listen to all network requests errors, something like this :

Ext.Ajax.on('requestexception', function(conn, response, options) {
    if (response.status === 555) {
        Ext.Msg.alert('test', 'test');
    }
});

The above code works only for requests via Ext.Ajax.request(), how to rewrite it so it could work also for form submits, url not found error etc.

On server side I have Spring MVC that dispatches all requests and if there is any error, the response status of 555 is returned.

form.submit({
     url: dispatcher.getUrl('savePlanRequest'),
     //headers: {'Content-Type':'multipart/form-data; accept-charset=utf-8'},
     scope: this,
     method: 'GET',
     params: {
         scan: scan_id,
         attachments: attachments_id,
         parcels: parcels_id
     },
     success: function(form, action) {
         this.fireEvent('plansaved', this);
         Ext.Msg.alert(i18n.getMsg('success'), i18n.getMsg('gsip.view.plans.NewPlanForm.success_info'))
     },
     failure: function(form, action) {
         console.log('failure');
         //Ext.Msg.alert(i18n.getMsg('failure'), action.result.msg);
     }
 });

Upvotes: 3

Views: 1384

Answers (2)

Ivan Novakov
Ivan Novakov

Reputation: 130

IMHO you don't need to override anything. You can place a listener on the Ext.Ajax singleton like it is described here:

Override Ext.data.Connection - Best Practice

Another option is to use the Ext.util.Observable.observe() function as described here:

http://www.sencha.com/forum/showthread.php?172269-Global-connection-handler-for-500-404-403-response-codes

Ext.util.Observable.observe(Ext.data.Connection);

Ext.data.Connection.on('requestexception', function(conn, response, options, eOpts) {
    //...handle it
});

Upvotes: 1

Izhaki
Izhaki

Reputation: 23586

This should work:

Ext.override( Ext.form.action.Submit, { 
    handleResponse : function( response ) {

        var form = this.form,
            errorReader = form.errorReader,
            rs, errors, i, len, records;

        if (errorReader) {
             rs = errorReader.read(response);
             success = rs.success;
             // Do something if success is false
        }

        this.callParent ( arguments ); 
    }
});

Have a look at the source code for the exact handleResponse() method from which I copied most of the code above.

Upvotes: 2

Related Questions