PDA

View Full Version : Managing server-side errors while loading a data store



le_chef
25 Feb 2012, 6:43 AM
Hello,

I'm moving from the basic carstore example in Sencha Designer Getting Started, where we have:


Ext.application {
model: ...
stores: ...

etc etc }

and store is AutoLoading, with an AJAX proxy, JSON reader, from a static file named:


data/cars.json


The store is bound to a grid, filled with data at opening.

if now I rename cars.json in


cars-off.json

then I reload the app and what I get is:

- the grid is empty
- Firebug shows me a 404 Not found coming from the server (of course)
- no JS exceptions thrown

Q#1: I would like to have my code to manage this scenario, for instance popping-up a window explaining the situation to the user.
But I can't see any exception to catch. Is there an event to listen to (I couldn't find any up to the task), which one ? Config options too, don't seem useful.

Q#2: now another scenario: cars.json gets back its correct name and so the server returns 200 - OK, but let us suppose the server wants to tell us that it had a problem somewhere trying to retrieve requested info.
AFAIK, it should return a: success: 'false' field as a metadata, in the JSON response.
Again, I'd like to manage this case doing something at client side, and given that the store is Autoload, where can I put my if test checking for success value ?

I apologize as my questions are so basic, but I couldn't find an answer in the doc despite a couple of days of reading. Thanks in advance.

mitchellsimoens
25 Feb 2012, 8:11 AM
For the store to know the request was a failure you have to change the status code to something like 400 or 500 or something. Then the failure callback will fire.


store.load({
failure : function() {}, //will fire if response is bad
success : function() {}, //will fire if response is good
callback : function() {} //will always fire
});

le_chef
25 Feb 2012, 8:19 AM
But if the store is Autoload as in this case, is not possible ?

(I can't set failure: success: properties for store.load, being Autoload)

Thank you

mitchellsimoens
25 Feb 2012, 8:22 AM
Then checkout the exception event.

le_chef
25 Feb 2012, 8:36 AM
But checking with Firebug, I can't see any uncaught exception. Are you sure that in this case there should be one ?

I've even added a: throw('fake');
for comparison, and Firebug complains just for that.

Summing up:
I'm causing at purpose a 404 - Not Found from the server just to test, using an autoloading store instantiated with Ext.Application { stores: ... } and all I can see is that the bound grid is empty.

mitchellsimoens
25 Feb 2012, 8:41 AM
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.proxy.Ajax-event-exception

The exception event is fired on the Store

le_chef
25 Feb 2012, 10:21 AM
Many thanks.

By experimenting I noticed that that event is triggered in two ways:

1- if HTTP STATUS is in 400 or 500 range
2- if JSON answer coming from the server contains success: 'false'

case 1:

the raw response is already deserialized in an object, for instance I can find response.status=404, very useful, so PROBLEM SOLVED thank you

case 2:
response contained some JSON, at least success: 'false'; the grid is still empty, maybe the JSON parser stopped when it encountered success: 'false'. It's not a problem for me, it makes sense.
Now I'd need to read the message property getting it from the JSON response, or get it from the ResultSet, to know what's up server side.
Have I to parse the raw data, maybe using read & readRecords, or there's a higher level / better approach (I'm inside the listener, all I have here is the raw response obj)?

Thank you again.