Skip to content

Instantly share code, notes, and snippets.

@charlypoly
Last active December 17, 2015 13:39
Show Gist options
  • Save charlypoly/5618929 to your computer and use it in GitHub Desktop.
Save charlypoly/5618929 to your computer and use it in GitHub Desktop.
Top level Request error handler - Similar to $.ajaxError in JQuery Example with PHP exception print in JSON by the remote server.
/*
* Top level Request error handler
* - Similar to $.ajaxError in JQuery
*/
Class.refactor(Request, {
__refactor_default_onComplete : null,
initialize : function(options) {
var opt_clone = Object.clone(options);
if (typeof opt_clone.onComplete != 'undefined') {
this.__refactor_default_onComplete = opt_clone.onComplete;
}
options['onComplete'] = this.__refactor_custom_onComplete.bind(this);
// for 40* HTTP errors custom handler at top level !
options['onFailure'] = this.__refactor_showErrorBox.bind(this);
this.previous(options);
},
__refactor_showErrorBox : function() {
this.$events= {}; //clear the callback queue, the hard way
// display error box here :)
},
__refactor_custom_onComplete : function() {
//arguments[0] is the JSON object, arguments[1] the raw response
if (typeof arguments[0] == "object") {
//we got an PHP exception
if (typeof arguments[0]['code'] != 'undefined' && typeof arguments[0]['exception'] != 'undefined' && typeof arguments[0]['type'] != 'undefined') {
this.$events= {}; //clear the callback queue, the hard way
if (arguments[0]['code'] == 403) { //this code is an example, depends on server config
window.location = 'login.php';
} else {
this.__refactor_showErrorBox();
}
}
} else {
if (this.__refactor_default_onComplete) {
this.__refactor_default_onComplete.call(this.__refactor_default_onComplete);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment