Sometime when developing AJAX applications our server side scripts have errors, which can be difficult to debug as we can't easily see them. The function below when added as an error back to the deferred chain will open new window and display the text/html the server sent. This means that if the server is configured to display useful information about any errors you will be able to see them without having to resort to proxy tools or network monitors.
A DEBUG flag is used so it can be easily turned off for production use.
function handleXMLHTTPRequestError(err) {
// Log the actual error
MochiKit.Logging.logError(err)
// Try and show a window if DEBUG is on and there was a request error
if (DEBUG && err.req) {
// show the page generating the error if it exists
if (err.req.responseText) {
var doc = window.open('', '_blank').document
doc.write(err.req.responseText);
doc.close();
// we could make this a partial function to give the window a
// useful title identifying the particular request
// e.g. doc.title = ...
}
}
return err;
}
// example usage
var DEBUG = true;
var d = MochiKit.Async.loadJSON('http://example.com/json_with_error');
d.addErrback(handleXMLHTTPRequestError)
A similar technique could be used to display any JSON/XML if errors were found processing it.
