Skip to content

Commit

Permalink
Fix duplicated error message when logging UnexpectedStatusException
Browse files Browse the repository at this point in the history
Not sure what's causing this. (Bluebird?)

Also add stack to custom HTTP exceptions.
  • Loading branch information
dstillman committed Apr 8, 2017
1 parent c0a2ec8 commit 3df66cc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
12 changes: 3 additions & 9 deletions chrome/content/zotero/xpcom/http.js
Expand Up @@ -14,6 +14,7 @@ Zotero.HTTP = new function() {
this.status = xmlhttp.status;
this.channel = xmlhttp.channel;
this.message = msg;
this.stack = new Error().stack;

// Hide password from debug output
//
Expand All @@ -40,29 +41,22 @@ Zotero.HTTP = new function() {
this.UnexpectedStatusException.prototype.is5xx = function () {
return this.status >= 500 && this.status < 600;
}
this.UnexpectedStatusException.prototype.toString = function() {
return this.message;
};

/**
* Exception returned if the browser is offline when promise* is used
* @constructor
*/
this.BrowserOfflineException = function() {
this.message = "XMLHttpRequest could not complete because the browser is offline";
this.stack = new Error().stack;
};
this.BrowserOfflineException.prototype = Object.create(Error.prototype);
this.BrowserOfflineException.prototype.toString = function() {
return this.message;
};

this.TimeoutException = function(ms) {
this.message = "XMLHttpRequest has timed out after " + ms + "ms";
this.stack = new Error().stack;
};
this.TimeoutException.prototype = Object.create(Error.prototype);
this.TimeoutException.prototype.toString = function() {
return this.message;
};

this.promise = function () {
Zotero.debug("Zotero.HTTP.promise() is deprecated -- use Zotero.HTTP.request()", 2);
Expand Down
22 changes: 18 additions & 4 deletions chrome/content/zotero/xpcom/utilities.js
Expand Up @@ -1431,10 +1431,24 @@ Zotero.Utilities = {
header = (obj.name ? obj.name + ' ' : '') + 'Exception';
}

return header + ': '
+ (obj.message ? ('' + obj.message).replace(/^/gm, level_padding).trim() : '')
+ '\n\n'
+ (obj.stack ? obj.stack.trim().replace(/^(?=.)/gm, level_padding) : '');
let msg = (obj.message ? ('' + obj.message).replace(/^/gm, level_padding).trim() : '');
if (obj.stack) {
let stack = obj.stack.trim().replace(/^(?=.)/gm, level_padding);

msg += '\n\n';

// At least with Zotero.HTTP.UnexpectedStatusException, the stack contains "Error:"
// and the message in addition to the trace. I'm not sure what's causing that
// (Bluebird?), but fix it here.
if (obj.stack.startsWith('Error:')) {
msg += obj.stack.replace('Error: ' + obj.message + '\n', '');
}
else {
msg += stack;
}
}

return header + ': ' + msg;
}

// Only dump single level for nsIDOMNode objects (including document)
Expand Down

0 comments on commit 3df66cc

Please sign in to comment.