Created
January 13, 2011 17:49
-
-
Save house9/778269 to your computer and use it in GitHub Desktop.
Util object with Generic Handling of ajax errors when using jquery and spinner on off
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
util object; setSpinnerNextTo, setSpinnerOff; globalAjaxErrorHandler | |
assumes you have a property on your namespace of env, most likely set via server side code | |
YOUR_NAMESPACE.env = "DEVELOPMENT"; // STAGING, PRODUCTION | |
YOUR_NAMESPACE.env = '<%= Rails.env.upcase %>'; // rails | |
YOUR_NAMESPACE.env = '<%= ApplicationState %>'; // asp, assuming you have an ApplicationState property :) | |
initialize the global error handling - http://api.jquery.com/ajaxError | |
jQuery(document).ajaxError(YOUR_NAMESPACE.util.globalAjaxErrorHandler); | |
right now it is set to parse on asp.net html errors, modify as needed for you needs | |
*/ | |
var undef; // undefined | |
var YOUR_NAMESPACE = YOUR_NAMESPACE || {}; | |
YOUR_NAMESPACE.util = { | |
// ************************************************************ | |
setSpinnerNextTo: function (controlID, topOffset, leftOffset) { | |
// log(controlID); | |
if (controlID !== undef && controlID !== null && controlID !== "") { | |
if (topOffset === undef || topOffset === null) { | |
topOffset = 0; | |
} | |
if (leftOffset === undef || leftOffset === null) { | |
leftOffset = 14; | |
} | |
var $control = jQuery(controlID); | |
if ($control.length === 0) { | |
return; | |
} | |
var offset = $control.offset(); | |
var top = offset.top + topOffset; | |
var left = offset.left + leftOffset + $control.width(); | |
// sometimes things are not what they seem, use the parent coordinates if needed | |
if (left < 0) { | |
left = jQuery(controlID).parent().offset().left + leftOffset; | |
} | |
// show it | |
jQuery('#spinner').css('top', top).css('left', left).show().css('z-index', 33010); | |
} | |
}, | |
// ************************************************************ | |
setSpinnerOff: function () { | |
jQuery('#spinner').hide(); | |
}, | |
// ************************************************************ | |
globalAjaxErrorHandler: function (e, xhr, settings, exception) { | |
var ct; | |
var fragment; | |
var lengthOfVisibleErrorMessage = 0; | |
var baseMessage = "Error: "; | |
var exceptionMessage = exception ? exception.message : xhr.responseText; | |
// display one of these | |
var friendly = baseMessage; | |
var local = baseMessage + xhr.status + ", " + xhr.statusText + "\n" + settings.url + "\n"; | |
if (xhr.status === 404) { | |
friendly = friendly + "Page not found"; | |
} | |
else { | |
friendly = friendly + " an exception has occurred\nPlease try again"; | |
} | |
if (YOUR_NAMESPACE) { | |
YOUR_NAMESPACE.util.setSpinnerOff(); | |
if (YOUR_NAMESPACE.env === "DEVELOPMENT") { | |
ct = xhr.getResponseHeader("content-type") || ""; | |
if (ct.indexOf('html') > -1) { | |
// parse our error message a bit, try to make it easier on devs | |
// based on the way asp generates html errors | |
fragment = $(exceptionMessage).find("h2"); | |
local = local + "\n" + fragment.text(); | |
fragment = $(exceptionMessage).find("pre"); | |
if (fragment.length > 0) { | |
fragment.each(function () { | |
local = local + "\n" + $(this).text(); | |
}); | |
} | |
} | |
else { | |
// json, text, xml, etc... add specific parsing later if needed | |
local = local + "\n" + exceptionMessage; | |
} | |
lengthOfVisibleErrorMessage = (local.length > 350) ? 350 : local.length; | |
alert(local.substring(0, lengthOfVisibleErrorMessage) + "...\n\n - Check firebug console for more info.\n - This message for development only."); | |
} | |
else { | |
alert(friendly); | |
} | |
} | |
else { | |
// log("FATAL: YOUR_NAMESPACE instance not found."); | |
alert(friendly); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment