samedi 18 octobre 2014

Behavior of the form actions 'Load' and 'Submit' in the case of JSON parse error

These two actions have some error handling in the onSuccess method, many types of errors are caught up and correctly reported for the further processing.

But if the response data format is damaged because of a program warning or other cause, the processing stops completely because of the JSON parse error.


Since the ExtJS version 3.x, I am overriding the onSuccess method of the Ext.form.action.Load and Ext.form.action.Submit, to correctly catch up the JSON parse error. Now, I would like to offer to take this in to the standard functionality.


Original code:



Code:



Ext.override(Ext.form.action.Load, {

onSuccess: function(response){
var result = this.processResponse(response),
form = this.form;
if (result === true || !result.success || !result.data) {
this.failureType = Ext.form.action.Action.LOAD_FAILURE;
form.afterAction(this, false);
return;
}
form.clearInvalid();
form.setValues(result.data);
form.afterAction(this, true);
},

}); // Ext.form.action.Load

Adjusted code:

Code:



/*
The reason of the override is that the parse error is not handled.
If the response JSON data is invalid, the processing stops completely
because of the JSON parse error.
We take the call processResponse in the 'try catch', then, we can
catch up the JSON parse error and correctly report it like other errors.
*/

Ext.override(Ext.form.action.Load, {

onSuccess: function(response){
var result = false;
var form = this.form;

try
{
result = this.processResponse(response);
}
catch(ex)
{
this.failureType = 'parse'; // could be Ext.form.action.Action.PARSE_ERROR;
form.afterAction(this, false);
return;
}


if (result !== true || !result.success || !result.data) {
this.failureType = Ext.form.action.Action.LOAD_FAILURE;
form.afterAction(this, false);
return;
}
form.clearInvalid();
form.setValues(result.data);
form.afterAction(this, true);
} // onSuccess

}); // Ext.form.action.Load

Original code:

Code:



Ext.override(Ext.form.action.Submit, {

onSuccess: function(response) {
var form = this.form,
success = true,
result = this.processResponse(response);
if (result !== true && !result.success) {
if (result.errors) {
form.markInvalid(result.errors);
}
this.failureType = Ext.form.action.Action.SERVER_INVALID;
success = false;
}
form.afterAction(this, success);
},

});



Adjusted code:


Code:



/*
The reason of the override is that the parse error is not handled.
If the response JSON data is invalid, the processing stops completely
because of the JSON parse error.
We take the call processResponse in the 'try catch', then, we can
catch up the JSON parse error and correctly report it like other errors.
*/

Ext.override(Ext.form.action.Submit, {

onSuccess: function(response) {
var form = this.form,
success = true,
result = false;

try
{
result = this.processResponse(response);
}
catch(ex)
{
this.failureType = 'parse'; // could be Ext.form.action.Action.PARSE_ERROR;
form.afterAction(this, false);
return;
}


if (result !== true && !result.success) {
if (result.errors) {
form.markInvalid(result.errors);
}
this.failureType = Ext.form.action.Action.SERVER_INVALID;
success = false;
}
form.afterAction(this, success);
}, // onSuccess

}); // Ext.form.action.Submit





Behavior of the form actions 'Load' and 'Submit' in the case of JSON parse error

Aucun commentaire:

Enregistrer un commentaire