PDA

View Full Version : Handle server errors like Fatal error, Warning, Notice on Ajax.request, form.submit



eliotik
12 Jan 2011, 11:51 PM
Hello. How can i catch server errors like Fatal error, Warning, Notice in php script while i do ext.ajax.request or form.submit?
listener actionfailed for form submit dont work
and
listener for ajax.request requestexception dont work
i know, you can say that js can not know about php script error, but for example jQuery when you use $.ajax() on ajax.event "error" can handle parse error, yes all php script error like Fatal errors, Warnings, Notices and other can be handled by ajax.event "error" (parsererror).
may be i dont know how do same thing in ext or may be its not possible with ext?
thank you for help.

eliotik
13 Jan 2011, 1:41 AM
i mean, how can i fix this when do ajax.request or form.submit:
:::response:::
missing ) in parenthetical
[Break on this error] <b>Fatal error</b>: Call to undefined...ient.php</b> on line <b>217</b><br />\n
ext-all-debug.js (?????? 4234)


doDecode = function(json){
return eval("(" + json + ")");
},


ofcourse i can do try/catch in the php script and return json response, and its may be will be right decision, but i want for sure to have ability to handle this type of errors on ajax.request and form.submit like in jquery .

eliotik
13 Jan 2011, 3:18 AM
for ajax.request i did next:


var ajax = new Ext.data.Connection({
listeners: {
'requestcomplete': {
fn: function(con, res, opt){
try {
Ext.util.JSON.decode(response.responseText);
} catch (e) {
Ext.Msg.show({
closable: false,
title: 'Error',
icon: Ext.Msg.ERROR,
msg: 'Bad response from server: '+response.responseText,
buttons: Ext.Msg.OK
});
}
},
scope: this
}
}
});

ajax.request({...})


and it works :)

but trouble left with form.submit . event 'actioncomplete' dont work.

dying_angel
5 Feb 2011, 6:44 PM
Any body know how to handle this ? can't seem to work it :(

i think its because in the XHR request it self is success ( http status 200, but the response it self is not in JSON format as expected )

The handler of the submit actin is in the Ext.form.Action.Submit class is executing these method : success -> processResponse -> handleResponse. the problem seems to come from this code : return Ext.decode(response.responseText) in the handleResponse method. this execution seems to fail, and the process just stopped, dont return error or continue the process.

dying_angel
5 Feb 2011, 7:23 PM
i think i've found a way, but has'nt tested it in other browser



Ext.override(Ext.form.Action.Submit, {
handleResponse : function(response){
if(this.form.errorReader){
var rs = this.form.errorReader.read(response);
var errors = [];
if(rs.records){
for(var i = 0, len = rs.records.length; i < len; i++) {
var r = rs.records[i];
errors[i] = r.data;
}
}
if(errors.length < 1){
errors = null;
}
return {
success : rs.success,
errors : errors
};
}

// return Ext.decode(response.responseText); // original code
try {
var xTmp = Ext.decode(response.responseText);
return xTmp;
} catch(exp) {
return false;
}
}
});

alien3d
6 Feb 2011, 2:53 AM
Ext.Ajax.request({
url: ' ',

method: 'GET',
success: function (response, options) {
x = Ext.decode(response.responseText);
if (x.success == 'true') {
Ext.MessageBox.alert('SYSTEM', x.message);
box.hide();
} else {
Ext.MessageBox.alert('SYSTEM', x.message);
}

},
failure: function (response, options) {
status_code = response.status;
status_message = response.statusText;
Ext.MessageBox.alert('system', escape(status_code) + ":" + status_message);
}
});



formPanel.getForm().submit({
waitMsg: waitMessageLabel,
params: {
method: 'save',
leafId: leafId
},
success: function (
form, action) {
var title = successLabel;
Ext.MessageBox.alert(
title, action.result.message);
formPanel.getForm().reset();


},
failure: function (form, action) {

if (action.failureType === Ext.form.Action.LOAD_FAILURE) {
alert(loadFailureMessageLabel);
} else if (action.failureType === Ext.form.Action.CLIENT_INVALID) {
// here will be error if duplicate code
alert(clientInvalidMessageLabel);
} else if (action.failureType === Ext.form.Action.CONNECT_FAILURE) {
Ext.Msg.alert(form.response.status + ' ' + form.response.statusText);
} else if (action.failureType === Ext.form.Action.SERVER_INVALID) {
Ext.Msg.alert(failureLabel, action.result.message);
}
}

});
hope help