PDA

View Full Version : metachange exception handling



cdomigan
13 Nov 2012, 1:24 PM
I can't seem to see errors thrown within a metachange listener.


metachange: function(store, meta) {
throw "META ERROR"; // No error thrown! Execution just stops.
}

Other async events ('load', etc) are throwing errors just fine in their listeners.

vietits
13 Nov 2012, 4:38 PM
First, you should make sure 'metachange' event is fired on your store. This event is fired when the returned data from server includes metaData property.

cdomigan
13 Nov 2012, 4:41 PM
It appears the root of the issue is that onMetaChange() is fired by readRecords() in the context of a try/catch block inside getResponseData() in Ext.data.reader.Json.

This catches the error and fires the "exception" event of the Reader, formatted with the assumption that it will only be handling an Ext.decode() (ie JSON parsing) error. This is clearly incorrect as there could also be any amount of erroneous code inside the metachange handler that will throw an error here.

Anyone have any ideas how to resolve this? See getResponseData() in Ext.data.reader.Json for the offending code.

vietits
13 Nov 2012, 5:08 PM
From the source code, the 'metachange' event is only fired if the returned data from server is in the right format and if it contains metaData property. If one of this condition is not satisfied, the event will not be fired. So, if because of any reason that the returned data is not in the right format that causes error when parsing it, the 'metachange' event will not be fired. Instead the 'exception' event will be fired.

cdomigan
13 Nov 2012, 5:10 PM
The "metachange" event is firing correctly. The problem is that errors originating from within "metachange" listeners are being swallowed by the try/catch block in getResponseData().

vietits
13 Nov 2012, 5:21 PM
Ah! Now I understand what you mean. Then one of the solutions to escape the try/catch block is to override the getResponseData().


Ext.define('Override.data.reader.Json', {
override: 'Ext.data.reader.Json',
getResponseData: function(response) {
var data, error;

try {
data = Ext.decode(response.responseText);
//return this.readRecords(data);
} catch (ex) {
error = new Ext.data.ResultSet({
total : 0,
count : 0,
records: [],
success: false,
message: ex.message
});
this.fireEvent('exception', this, response, error);
Ext.Logger.warn('Unable to parse the JSON returned by the server');
return error;
}
return this.readRecords(data);
}
}

cdomigan
13 Nov 2012, 5:44 PM
Works a treat! Thanks! :)