PDA

View Full Version : Ajax store exception doesn't fire



mishoboss
3 Jan 2012, 7:53 AM
Ajax store exception doesn't fire. Am I doing it wrong or it's a bug in Sencha 2 PR3?


var sitemapsStore = new Ext.data.Store({
fields: ['name', 'link'],
autoLoad: true,
listeners: {
exception: function (proxy, response, op, opt) {
alert("Error");
}
},
proxy: {
type: 'ajax',
url: '/rest/sitemaps',
headers: {
'Accept': 'application/json',
},
noCache: true,
limitParam: undefined,
startParam: undefined,
pageParam: undefined,



reader: {
type: 'json',
root: 'sitemap'
}
}

});

mitchellsimoens
3 Jan 2012, 7:55 AM
What are you returning? A status code of 500 or anything like that?

mishoboss
3 Jan 2012, 9:21 AM
Yes - 503 Service Temporarily Unavailable

bweiler
3 Jan 2012, 1:11 PM
Move the listeners under the proxy. It won't catch all errors, but at least it catches some.



proxy: {
type: 'ajax',
url: '/rest/sitemaps',
headers: {
'Accept': 'application/json',
},
noCache: true,
limitParam: undefined,
startParam: undefined,
pageParam: undefined,

reader: {
type: 'json',
root: 'sitemap'
},

listeners: {
exception: function (proxy, response, op, opt) {
alert("Error");
}
},
}

mishoboss
3 Jan 2012, 11:45 PM
That was the first thing I've tried. It didn't work.

What I do is checking success parameter of the load event:


listeners: {
exception: function (proxy, response, op, opt) {
alert('Error');
},
load: function(store, records, success){
if(!success){
alert('Error');
}....

bweiler
4 Jan 2012, 7:36 AM
I ran a couple of error tests and my ajax proxy exception handler caught a 500 error. I also tried to send back text instead of json, but the exception handler did not fire in that case.

The only other difference between your code and mine is that I put the proxy in my model and not my store. My understanding is that this should not make a difference.

SButts
19 Mar 2012, 8:44 PM
Has there been a resolution to this issue? I am having the same situation, the exception event is not firing and I need to know how to gracefully handle data issues.

bweiler
20 Mar 2012, 10:51 AM
My proxy exception handler fires on 500 errors and timeouts.



proxy.on({'exception': function(proxy, resp, oper, opts) {


if (resp.timedout) {
Ext.Msg.alert("Error", "Request timed out.", Ext.emptyFn);
}
else {
console.log(resp.statusText, ": ", resp.responseText);
}
}
});


If you need finer grained control of errors, try the Ext.Ajax.request with success and failure handlers.

SButts
21 Mar 2012, 6:49 AM
Thanks for the response. How would I "simulate" a 500 or time out error? I am new to web development and apologize if these questions seem simple.

I did find that I could catch 404 errors in the load event by checking the operation parameter and that catches those types of errors.

bweiler
21 Mar 2012, 10:03 AM
Assuming you have a local environment setup for testing you can leave a semicolon off the end of a line in your php code which will cause a 500 error and you can set the timeout value in your sencha touch proxy to 1 ms which should cause a timeout.

SButts
21 Mar 2012, 11:20 AM
Got it working, thanks. I know feel a little better about catching "unexpected" errors.

roycyang
14 Apr 2012, 7:18 AM
I'm been grappling with this issue for a couple of days. How do we test for the instance where we can't get any file (offline, server down), hence so server error code.

I have no idea where that event would be fired.

SButts
16 Apr 2012, 6:17 AM
Here is what I have done. I don't know if this is the proper or recommended way to do this, but this is what I have working right now. I just started working with Sencha and Javascript 2 months ago with no previous web experience, so take it for what it is worth. If you come up with a better way of handling this please let me know.

1st this is how I call my store to load the data.


// Load the store.
var store = Ext.StoreMgr.get('PatientInfoStore');
var proxy = store.getProxy();

proxy.setUrl(baseURL + 'getinfo.php');

// Setup the parameters
proxy.setExtraParam('patientId',patientId);
proxy.setExtraParam('archived',archived);

// Setup my global AjaxStoreException handler
proxy.on('exception',MyApp.app.AjaxStoreException,this,{single:true});

// Setup my custom handler for the load event
store.on('load',this.onInfoStoreLoad,this,{single:true});

Ext.Viewport.setMasked(true);

// finally call load
store.load();


This is the AjaxStoreException handler code. The parameters are server, response, operation and options.


Ext.Viewport.setMasked(false);

if (response.timedout) {
Ext.Msg.alert("Error", "Request timed out.", Ext.emptyFn);
}
else {
Ext.Msg.alert('Exception',response.responseText);
}


This is my onInfoStoreLoad code. The parameters are store, records, successful, operation and eOpts.


var reader = store.getProxy().getReader();

// Check the operation.exception property FIRST so that if there
// was an error any "old" JSON message are NOT displayed.
if (operation.exception) {
Ext.Msg.alert('Exception','Status: ' +
operation.error.status + ' - ' +
operation.error.statusText);
return;
}

// This is when your script returns not successful
if (!successful) {
// msg is the message property of the stores reader
Ext.Msg.alert('Error Loading Data',reader.rawData.msg);
}
else {
// It is possible for successful to be true and NO dat has been returned
// this happens when there is an error in the php file or
// nothing is returned.
if (reader.rawData.responseText === "") {
Ext.Msg.alert('Exception','Request: Returned no Data');
console.log('URL:' + reader.rawData.request.options.url);
return;
}

// put code to do your process here, you should have your data

}