2

How can I prevent ajax failure popups and instead route the failure to the console log instead? Is this something I can do with Drupals implementation of jQuery?

2 Answers 2

4

You can check Prevent JS alerts module..

Project page says

This module is an answer to the issue: #1232416: Drupal alerts "An AJAX HTTP request terminated abnormally" during normal site operation, confusing site visitors/editors, which describes a big problem for end users: JavaScript alert();'s for end users on AJAX errors by Drupal core. Perhaps it will one day become obsolete, when this bug is fixed. End users are often frightened of these errors and don't know, if they have "destroyed the internet" ;)

This module completely suppresses ALL JavaScript alerts and instead prints them to console via console.error! Important: It does NOT ONLY suppress the core alerts, but also custom alerts.

1

The prevent_js_alerts module is a heavy handed approach to handling ajax errors. If you want to intercept some errors, you can try something like this in your module or theme, which gives you a way of conditionally intercepting some messages, and letting Drupal do the default for everything else.

(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.
      Drupal.ajaxErrorDefault = Drupal.ajaxError;
      Drupal.ajaxError = function (xmlhttp, uri, customMessage) {
        var message = '';
        try {
          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 {
          // Passthru to the default Drupal.ajaxError if we don't want to intercept it.
          return Drupal.ajaxErrorDefault(xmlhttp, uri, customMessage);
        }
      }
    }
  };

})(jQuery);

This code is a variation of some project code, and may not work without some debugging. This solution is also similar to the module ajax_error_behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.