diff --git a/core/misc/drupal.js b/core/misc/drupal.js index 458d385..0583382 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -18,28 +18,6 @@ if (window.jQuery) { "use strict"; /** - * Custom error type thrown after attach/detach if one or more behaviors failed. - * - * @param list - * An array of errors thrown during attach/detach. - * @param event - * A string containing either 'attach' or 'detach'. - */ - function DrupalBehaviorError(list, event) { - this.name = 'DrupalBehaviorError'; - this.event = event || 'attach'; - this.list = list; - // Makes the list of errors readable. - var messageList = []; - messageList.push(this.event); - for (var i = 0, il = this.list.length; i < il; i++) { - messageList.push(this.list[i].behavior + ': ' + this.list[i].error.message); - } - this.message = messageList.join(' ; '); - } - DrupalBehaviorError.prototype = new Error(); - - /** * Attach all registered behaviors to a page element. * * Behaviors are event-triggered actions that attach to page elements, enhancing @@ -80,22 +58,30 @@ if (window.jQuery) { Drupal.attachBehaviors = function (context, settings) { context = context || document; settings = settings || drupalSettings; - var i, errors = [], behaviors = Drupal.behaviors; - // Execute all of them. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].attach === 'function') { + var errors = []; + var behaviors = Drupal.behaviors; + + function runBehavior(name) { + if (typeof behaviors[name].attach === 'function') { // Don't stop the execution of behaviors in case of an error. try { - behaviors[i].attach(context, settings); + behaviors[name].attach(context, settings); } catch (e) { - errors.push({ behavior: i, error: e }); + errors.push(e); } } } + + function throwError(error) { + setTimeout(function () { throw error; }, 0); + } + + // Execute all of them. + Object.keys(behaviors).forEach(runBehavior); // Once all behaviors have been processed, inform the user about errors. if (errors.length) { - throw new DrupalBehaviorError(errors, 'attach'); + errors.forEach(throwError); } }; @@ -146,22 +132,29 @@ if (window.jQuery) { context = context || document; settings = settings || drupalSettings; trigger = trigger || 'unload'; - var i, errors = [], behaviors = Drupal.behaviors; - // Execute all of them. - for (i in behaviors) { - if (behaviors.hasOwnProperty(i) && typeof behaviors[i].detach === 'function') { + var errors = []; + var behaviors = Drupal.behaviors; + + function runBehavior(name) { + if (typeof behaviors[name].detach === 'function') { // Don't stop the execution of behaviors in case of an error. try { - behaviors[i].detach(context, settings, trigger); + behaviors[name].detach(context, settings, trigger); } catch (e) { - errors.push({ behavior: i, error: e }); + errors.push(e); } } } + + function throwError(error) { + setTimeout(function () { throw error; }, 0); + } + // Execute all of them. + Object.keys(behaviors).forEach(runBehavior); // Once all behaviors have been processed, inform the user about errors. if (errors.length) { - throw new DrupalBehaviorError(errors, 'detach:' + trigger); + errors.forEach(throwError); } };