Skip to content

Instantly share code, notes, and snippets.

@simesy
Created January 25, 2017 02:41
Show Gist options
  • Save simesy/72c2a1feee9935e790315d92e7fd45bd to your computer and use it in GitHub Desktop.
Save simesy/72c2a1feee9935e790315d92e7fd45bd to your computer and use it in GitHub Desktop.
(function ($) {
Drupal.behaviors.catchSomeDrupalAjaxAlerts = {
attach: function(context, settings) {
// Replace this simple core function with one that won't blurt empty messages.
Drupal.displayAjaxError = function (message) {
if (message && !Drupal.beforeUnloadCalled) {
alert(message);
}
}
// Intercept ajaxError to handle certain messages.
// Passthru to the default Drupal.ajaxError if we don't want to intercepts.
Drupal.ajaxErrorDefault = Drupal.ajaxError;
Drupal.ajaxError = function (xmlhttp, uri, customMessage) {
try {
var message = xmlhttp.error.message;
}
catch (e) {}
// Handle various known cases.
if (uri.indexOf('file/ajax') !== -1 && message == 'Unexpected end of JSON input') {
// DO SOMETHING ELSE HERE
$('body').append('<div class="error">SOMETHING HAPPENED PLEASE CONTACT 911</div>');
return false;
}
else {
// Default to pass back to Drupal's handler.
return Drupal.ajaxErrorDefault(xmlhttp, uri, customMessage);
}
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment