$(function () { if (typeof (collegiatelink) == 'undefined') { collegiatelink = {}; } if (typeof (collegiatelink.ajax) == 'undefined') { collegiatelink.ajax = {}; } else { return; } collegiatelink.ajax.errorHandler = function(jqxhr, textStatus, errorThrown) { var customErrorHandler = jqxhr[jqxhr.status]; if(customErrorHandler) { if(_.isFunction(customErrorHandler)) { customErrorHandler(); return; } else collegiatelink.ajax.errorHandler.flashMessage(customErrorHandler); } else { switch(jqxhr.status) { case 401: collegiatelink.ajax.errorHandler.growlMessage('You are not authorized to perform this action.'); break; case 404: collegiatelink.ajax.errorHandler.growlMessage('The requested item was not found.'); break; case 400: case 500: collegiatelink.ajax.errorHandler.handleModelStateErrors(jqxhr); break; default: collegiatelink.ajax.errorHandler.growlMessage('There was an error processing your request.'); } } }; collegiatelink.ajax.errorHandler.flashMessage = function(message) { $.Flash("error", message); }; collegiatelink.ajax.errorHandler.growlMessage = function (message) { if (core && core.growl) core.growl.error(message); else flashMessage(message); }; collegiatelink.ajax.errorHandler.flashErrors = function (errors) { if (!errors.length) return; $.Flash("error", ''); }; collegiatelink.ajax.errorHandler.handleModelStateErrors = function (jqxhr) { if (jqxhr.responseText != '' || jqxhr.data != null) { try { var errors = []; var payload = jqxhr.data != null ? jqxhr.data : jqxhr.responseText != '' ? $.parseJSON(jqxhr.responseText) : {}; if(payload.ModelState) { _.each(payload.ModelState, function(error) { errors.push(error); }); collegiatelink.ajax.errorHandler.flashErrors(errors); } } catch (error) { collegiatelink.ajax.errorHandler.flashMessage(jqxhr.responseText); } } }; });