Improve error handling ...

This commit is contained in:
Paulo Gustavo Veiga 2012-11-11 20:07:56 -03:00
parent 3f2f23d3c1
commit 751692837c

View File

@ -63,27 +63,30 @@ mindplot.RESTPersistenceManager = new Class({
}, },
onFailure:function (xhr) { onFailure:function (xhr) {
console.log("onFailure....");
var responseText = xhr.responseText; var responseText = xhr.responseText;
var error = null; var userMsg = {severity:"ERROR", message:$msg('SAVE_COULD_NOT_BE_COMPLETED')};
var contentType = this.getHeader("Content-Type"); var contentType = this.getHeader("Content-Type");
if (contentType != null && contentType.indexOf("application/json") != -1) { if (contentType != null && contentType.indexOf("application/json") != -1) {
var serverMsg = null;
try { try {
error = JSON.decode(responseText); serverMsg = JSON.decode(responseText);
events.onError(persistence._buildError(error)); serverMsg = serverMsg.globalSeverity ? serverMsg : null;
} catch (e) { } catch (e) {
events.onError(persistence._buildError()); // Message could not be decoded ...
throw new Error("Unexpected error saving. Error response is not json object:" + responseText);
} }
} else { userMsg = persistence._buildError(serverMsg);
var msg = {severity:"ERROR", message:$msg('SAVE_COULD_NOT_BE_COMPLETED')};
if (this.status == 405) {
msg = {severity:"ERROR", message:$msg('SESSION_EXPIRED')};
}
events.onError(msg);
}
} else {
if (this.status == 405) {
userMsg = {severity:"ERROR", message:$msg('SESSION_EXPIRED')};
}
}
events.onError(userMsg);
// Only for debug. Remove.
throw new Error("responseText:" + responseText + ",status:" + this.status);
}, },
headers:{"Content-Type":"application/json", "Accept":"application/json"}, headers:{"Content-Type":"application/json", "Accept":"application/json"},