Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExstJs Store callback proper error handling

Tags:

extjs

I want to receive a personalized error message when trying to load a store and my connection times out or the DB it's not accessible ... when I do an Ajax Request is very easy because I get "response" as a parameter either in success or failure ...

Ext.Ajax.request({
   url: 'ajax_demo/sample.json',
   success: function(response, opts) {
      var obj = Ext.decode(response.responseText);
      console.dir(obj);
   },
   failure: function(response, opts) {
      console.log('server-side failure with status code ' + response.status);
   }
});

But I'm facing problems to do the same when trying to load a store, I've defined a callback function but I only receive records, operation and success.

store.load({
        callback : function(records, options, success) {
            if (!success) {

              // what can I do here to show personalized error sent from server
            }
        }
    });

So, what is the proper way to handle a response like this to show it to the user?

{"success": false, "msg": "SomeExceptionFromServer"}

Best regards

like image 369
code4jhon Avatar asked Nov 01 '22 08:11

code4jhon


1 Answers

When success is false operation never gets a response property instead it gets a getError method, but you should define a messageProperty in the proxyReader in order to work.

Example:

Ext.define("SC.store.SegurosCancelacionStore", {
    extend: "Ext.data.Store",
    model: "SC.model.PersonaSeguro",
    proxy: {
        timeout: 90000,
        actionMethods: {
            read   : 'POST'
        },
        type: "ajax",
        url: "../SegurosFinsolCancelacionServlet",
        reader: {
            type: "json",
            root: "seguros",
            messageProperty : 'msjError' //without this, it doesn't work
        }
    },
    autoLoad: false
});

Implementation:

storeSegurosCancelacion.load({
                params: {
                    'sucursal':sucursal,
                    'persona': persona
                },
                callback:function(records, operation, success){
                    msg.hide();
                    if(success == true){
                        if(records.length == 0){
                         Ext.Msg.alert('Resultado', 'No se ha encontrado información');
                        }
                    }
                    if(success == false){
                        try{
                             Ext.Msg.alert('Error', operation.getError()); // way more elegant than ussing rawData etc ...
                        }catch(e){
                                Ext.Msg.alert('Error', 'Error  inesperado en el servidor.');
                        }
                    }
                }
            });

Best regards

like image 196
code4jhon Avatar answered Jan 04 '23 14:01

code4jhon