From c80bde1f6008f142912c56a962a4d49ed75aa4dc Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 22 Nov 2017 14:42:32 +0000 Subject: [PATCH] Fix logging of DecryptionErrors to be more useful We were relying on being able to override toString in DecryptionError, which (a) doesn't work thanks to https://github.com/babel/babel/issues/3083, and (b) was a bit naughty anyway. Instead, just add a detailedString property and use that. --- src/crypto/algorithms/base.js | 37 +++++++++++++++-------------------- src/models/event.js | 4 ++-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/crypto/algorithms/base.js b/src/crypto/algorithms/base.js index c74e81f00..1f2434991 100644 --- a/src/crypto/algorithms/base.js +++ b/src/crypto/algorithms/base.js @@ -179,31 +179,26 @@ class DecryptionError extends Error { constructor(msg, details) { super(msg); this.name = 'DecryptionError'; - this.details = details; - } - - /** - * override the string used when logging - * - * @returns {String} - */ - toString() { - let result = this.name + '[msg: ' + this.message; - - if (this.details) { - result += ', ' + - Object.keys(this.details).map( - (k) => k + ': ' + this.details[k], - ).join(', '); - } - - result += ']'; - - return result; + this.detailedString = _detailedStringForDecryptionError(this, details); } } export {DecryptionError}; // https://github.com/jsdoc3/jsdoc/issues/1272 +function _detailedStringForDecryptionError(err, details) { + let result = err.name + '[msg: ' + err.message; + + if (details) { + result += ', ' + + Object.keys(details).map( + (k) => k + ': ' + details[k], + ).join(', '); + } + + result += ']'; + + return result; +} + /** * Exception thrown specifically when we want to warn the user to consider * the security of their conversation before continuing diff --git a/src/models/event.js b/src/models/event.js index 3a4e87f59..50474e511 100644 --- a/src/models/event.js +++ b/src/models/event.js @@ -424,7 +424,7 @@ utils.extend(module.exports.MatrixEvent.prototype, { // decryption error, but we have a retry queued. console.log( `Got error decrypting event (id=${this.getId()}: ` + - `${e.message}), but retrying`, + `${e}), but retrying`, ); continue; } @@ -432,7 +432,7 @@ utils.extend(module.exports.MatrixEvent.prototype, { // decryption error, no retries queued. Warn about the error and // set it to m.bad.encrypted. console.warn( - `Error decrypting event (id=${this.getId()}): ${e}`, + `Error decrypting event (id=${this.getId()}): ${e.detailedString}`, ); res = this._badEncryptedMessage(e.message);