6

I am trying to handle an exception in an Ext.data.Store instance when creating a new Ext.data.Record. When the server responds with the following json:

{"success": false, "message": "some text"}

I get an exception of type 'request', even though the server returns an HTTP 200 Response!

To get a 'remote' error I have to create an object with the root property

({
    "success": false,
    "message": "some text",
    "data": {
        "PositionId": "00000000-0000-0000-0000-000000000000",
        "Name": "123"
    }
})

...but I don't want this. Is there any way to change this behaviour?

Also, when I insert a record in the store, it is automatically added to the associated grid, but if an error occurs it remains there, so I need to reload store on every error. Is there any better way to do this?

NT3RP
  • 15,262
  • 9
  • 61
  • 97
kalan
  • 1,752
  • 4
  • 20
  • 35
  • Can you build on your question a bit? For example, where you are getting the 'request' or 'remote' errors you are seeing? If it is in a 'loadexception' listener on the store, then it should only have exception types of 'response' and 'remote'. Where are you seeing a 'request' error? If you can provide code examples, that will better help to answer your question. – Sean Adkinson Mar 11 '11 at 16:30

3 Answers3

11

You should catch one of the two Store events:

  1. loadexception (deprecated)
  2. exception

For example you could:

// make the store
var myStore = new Ext.data.Store({...});
// catch loading exceptions
myStore.on('exception',function( store, records, options ){
    // do something about the record exception
},this);
// load store
myStore.load();

You could also just use the success and failure events from the store to take action based on the success flag.

Remolten
  • 2,614
  • 2
  • 25
  • 29
Joseph Lust
  • 19,340
  • 7
  • 85
  • 83
4

Finally, I've found out that if I send back empty data it works as expected. So I don't need to send back any fictional data, my server response is:

({
    "success": false,
    "message": "some text",
    "data": {}
})
kalan
  • 1,752
  • 4
  • 20
  • 35
  • can i have little bit more details? i tried the same but still have to reload grid again :( – Leonid Feb 24 '12 at 13:42
  • 1
    I think when you don't have root property in the response, reader fails and load event doesn't fire. But when you send empty results event "load" triggers and you may check returned json (in ext4.2 at least) like this: this.getProxy().getReader().rawData; – Greg May 02 '13 at 08:32
  • when success is false you can't access response property on operation object. Please see http://www.sencha.com/forum/showthread.php?196013-access-operation.response-when-success-false – code4jhon May 26 '14 at 18:13
0

when success is false operation doesn't have a response property. This thread explains it very clairly!

http://www.sencha.com/forum/showthread.php?196013-access-operation.response-when-success-false

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 @code4jhon

code4jhon
  • 5,725
  • 9
  • 40
  • 60