diff --git a/core/misc/ajax.js b/core/misc/ajax.js index d599799..041550d 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -71,6 +71,55 @@ Drupal.behaviors.AJAX = { }; /** + * Build an error message from an Ajax response. + */ +Drupal.ajaxError = function (xmlhttp, uri) { + console.error(Drupal.t("An AJAX HTTP error occurred."), uri, xmlhttp); + + // Return an object we can print as a string This avoid processing the several + // try/catch when there is no need to. + return { + uri: uri, + xmlhttp: xmlhttp, + toString: function () { + var statusCode, statusText, pathText, responseText, readyStateText, message; + if (xmlhttp.status) { + statusCode = "\n" + Drupal.t("An AJAX HTTP error occurred.") + "\n" + Drupal.t("HTTP Result Code: !status", {'!status': xmlhttp.status}); + } + else { + statusCode = "\n" + Drupal.t("An AJAX HTTP request terminated abnormally."); + } + statusCode += "\n" + Drupal.t("Debugging information follows."); + pathText = "\n" + Drupal.t("Path: !uri", {'!uri': uri} ); + statusText = ''; + // In some cases, when statusCode == 0, xmlhttp.statusText may not be defined. + // Unfortunately, testing for it with typeof, etc, doesn't seem to catch that + // and the test causes an exception. So we need to catch the exception here. + try { + statusText = "\n" + Drupal.t("StatusText: !statusText", {'!statusText': $.trim(xmlhttp.statusText)}); + } + catch (e) {} + + responseText = ''; + // Again, we don't have a way to know for sure whether accessing + // xmlhttp.responseText is going to throw an exception. So we'll catch it. + try { + responseText = "\n" + Drupal.t("ResponseText: !responseText", {'!responseText': $.trim(xmlhttp.responseText) } ); + } catch (e) {} + + // Make the responseText more readable by stripping HTML tags and newlines. + responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,""); + responseText = responseText.replace(/[\n]+\s+/g,"\n"); + + // We don't need readyState except for status == 0. + readyStateText = xmlhttp.status == 0 ? ("\n" + Drupal.t("ReadyState: !readyState", {'!readyState': xmlhttp.readyState})) : ""; + + return statusCode + pathText + statusText + responseText + readyStateText; + } + }; +}; + +/** * Ajax object. * * All Ajax objects on a page are accessible through the global Drupal.ajax @@ -252,7 +301,7 @@ Drupal.ajax.prototype.eventResponse = function (element, event) { // Unset the ajax.ajaxing flag here because it won't be unset during // the complete response. ajax.ajaxing = false; - alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message); + console.error("An error occurred while attempting to process " + ajax.options.url, e.message, ajax, e); } // For radio/checkbox, allow the default event. On IE, this means letting @@ -441,7 +490,7 @@ Drupal.ajax.prototype.getEffect = function (response) { * Handler for the form redirection error. */ Drupal.ajax.prototype.error = function (response, uri) { - alert(Drupal.ajaxError(response, uri)); + Drupal.ajaxError(response, uri); // Remove the progress element. if (this.progress.element) { $(this.progress.element).remove(); diff --git a/core/misc/autocomplete.js b/core/misc/autocomplete.js index 8b0dd72..2fe6c9f 100644 --- a/core/misc/autocomplete.js +++ b/core/misc/autocomplete.js @@ -312,7 +312,7 @@ Drupal.ACDB.prototype.search = function (searchString) { } }, error: function (xmlhttp) { - alert(Drupal.ajaxError(xmlhttp, db.uri)); + Drupal.ajaxError(xmlhttp, db.uri); } }); }, this.delay); diff --git a/core/misc/drupal.js b/core/misc/drupal.js index 75767d5..42d6689 100644 --- a/core/misc/drupal.js +++ b/core/misc/drupal.js @@ -5,7 +5,7 @@ jQuery.noConflict(); // JavaScript should be made compatible with libraries other than jQuery by // wrapping it in an anonymous closure. -(function ($, Drupal, window, document, undefined) { +(function ($, Drupal, window, document, console, undefined) { "use strict"; @@ -112,6 +112,15 @@ Drupal.detachBehaviors = function (context, settings, trigger) { } }; +// Create dummy functions if browser console is not available. +if (typeof console !== 'object') { + console = { + log: function () {}, + warn: function () {}, + error: function () {} + }; +} + /** * Encode special characters in a plain-text string for display as HTML. * @@ -338,46 +347,6 @@ Drupal.getSelection = function (element) { return { 'start': element.selectionStart, 'end': element.selectionEnd }; }; -/** - * Build an error message from an Ajax response. - */ -Drupal.ajaxError = function (xmlhttp, uri) { - var statusCode, statusText, pathText, responseText, readyStateText, message; - if (xmlhttp.status) { - statusCode = "\n" + Drupal.t("An AJAX HTTP error occurred.") + "\n" + Drupal.t("HTTP Result Code: !status", {'!status': xmlhttp.status}); - } - else { - statusCode = "\n" + Drupal.t("An AJAX HTTP request terminated abnormally."); - } - statusCode += "\n" + Drupal.t("Debugging information follows."); - pathText = "\n" + Drupal.t("Path: !uri", {'!uri': uri} ); - statusText = ''; - // In some cases, when statusCode == 0, xmlhttp.statusText may not be defined. - // Unfortunately, testing for it with typeof, etc, doesn't seem to catch that - // and the test causes an exception. So we need to catch the exception here. - try { - statusText = "\n" + Drupal.t("StatusText: !statusText", {'!statusText': $.trim(xmlhttp.statusText)}); - } - catch (e) {} - - responseText = ''; - // Again, we don't have a way to know for sure whether accessing - // xmlhttp.responseText is going to throw an exception. So we'll catch it. - try { - responseText = "\n" + Drupal.t("ResponseText: !responseText", {'!responseText': $.trim(xmlhttp.responseText) } ); - } catch (e) {} - - // Make the responseText more readable by stripping HTML tags and newlines. - responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi,""); - responseText = responseText.replace(/[\n]+\s+/g,"\n"); - - // We don't need readyState except for status == 0. - readyStateText = xmlhttp.status == 0 ? ("\n" + Drupal.t("ReadyState: !readyState", {'!readyState': xmlhttp.readyState})) : ""; - - message = statusCode + pathText + statusText + responseText + readyStateText; - return message; -}; - // Class indicating that JS is enabled; used for styling purpose. $('html').addClass('js'); @@ -407,4 +376,4 @@ Drupal.theme.prototype = { } }; -})(jQuery, Drupal, this, this.document); +})(jQuery, Drupal, this, this.document, window['console']); diff --git a/core/misc/progress.js b/core/misc/progress.js index 343b9ad..086ae25 100644 --- a/core/misc/progress.js +++ b/core/misc/progress.js @@ -87,7 +87,8 @@ Drupal.progressBar.prototype.sendPing = function () { pb.timer = setTimeout(function () { pb.sendPing(); }, pb.delay); }, error: function (xmlhttp) { - pb.displayError(Drupal.ajaxError(xmlhttp, pb.uri)); + var error = Drupal.ajaxError(xmlhttp, pb.uri); + pb.displayError(error.toString()); } }); }