0

We have a use case where we need to block Drupal's core ajax error handling from alerting users (we're handling the error reporting on our own). Previously another developer had commented out a line in the core ajax.js file, to prevent Drupal from spawning the alert box, but I'd like to handle it without touching core.

From the core, drupal.js:

/**
 * Displays a JavaScript error from an Ajax response when appropriate to do so.
 */
   Drupal.displayAjaxError = function (message) {
  // Skip displaying the message if the user deliberately aborted (for example,
  // by reloading the page or navigating to a different page) while the Ajax
  // request was still ongoing. See, for example, the discussion at
  // http://stackoverflow.com/questions/699941/handle-ajax-error-when-a-user-
  // clicks-refresh.
  if (!Drupal.beforeUnloadCalled) {
    alert(message);
  }
};

My current fix, is to override the Drupal.displayAjaxError function and change the Drupal.beforeUnloadCalled property that determines whether or not to alert the error:

   var ajax_error_backup = Drupal.displayAjaxError;
   Drupal.displayAjaxError = function (message) {
     Drupal.beforeUnloadCalled = true;
     ajax_error_backup(message);
   };

My question, is whether or not this is an appropriate fix? I know that I could also override the function and just leave it empty - costing fewer lines, and not invoking another call to the original function (and saving the object I've created by backing up the original in ajax_error_backup). Am I adding complexity to keep things tidy, or should I just override with:

  Drupal.displayAjaxError = function (message) {
    //empty
  };

To clarify - the desire is to never have this ajax alert occur, so there's not functional difference between my desire to keep things neat/tidy, and just overriding the function with a blank one - there isn't a case where want this alert to succeed.

Thanks in advance for helping this old dog think through something with fresh eyes.

slothluvchunk
  • 382
  • 2
  • 9

1 Answers1

0

In this case, there isn't one option that seems to be clearly better than the other. It should be handled on a case by case basis, and in this case, either of the methods really is adequate.

I personally opted for using the slightly more expensive method of overriding the function and calling it back, because I felt that it might be somewhat more future-proof:

   var ajax_error_backup = Drupal.displayAjaxError;
   Drupal.displayAjaxError = function (message) {
     Drupal.beforeUnloadCalled = true;
     ajax_error_backup(message);
   };

If Drupal were to extend the function on their end in the future, there might be another condition that we wouldn't want to override.

Overriding with the empty function would be the cheapest, but would also potentially be a bit heavy handed.

It seems that either approach is valid, and is probably best handled case-by-case.

slothluvchunk
  • 382
  • 2
  • 9