From bf6c2b93196f9271d50c66e8ac86b7af1d3cc41a Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Fri, 24 Nov 2017 16:12:37 -0600 Subject: [PATCH 1/6] Added a test verifying innerError --- src/openpgp.js | 5 ++++- test/general/openpgp.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/openpgp.js b/src/openpgp.js index 5a2a5e5a..12f7a971 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -551,7 +551,10 @@ function onError(message, error) { // log the stack trace if (config.debug) { console.error(error.stack); } // rethrow new high level error for api users - throw new Error(message + ': ' + error.message); + const newError = new Error(message + ': ' + error.message); + newError.stack += '\n' + error.stack; + newError.innerError = error; + throw newError; } /** diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 01295fe6..eab0d9fe 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1200,6 +1200,20 @@ describe('OpenPGP.js public api tests', function() { }); }); }); + + describe('Errors', function() { + it('Errors should contain innerError', function(done) { + openpgp.encrypt({ + data: new Uint8Array([0x01, 0x01, 0x01]), + passwords: null + }) + .then(() => done(new Error('Error expected.'))) + .catch(function(error){ + expect(error.innerError).to.exist; + done(); + }); + }); + }) } }); From 567d2122044424c5d89d53b05f61d46cb208722d Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Fri, 24 Nov 2017 16:16:42 -0600 Subject: [PATCH 2/6] Dont join stacks --- src/openpgp.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/openpgp.js b/src/openpgp.js index 12f7a971..e2f9bd5d 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -552,7 +552,6 @@ function onError(message, error) { if (config.debug) { console.error(error.stack); } // rethrow new high level error for api users const newError = new Error(message + ': ' + error.message); - newError.stack += '\n' + error.stack; newError.innerError = error; throw newError; } From f4cf6d7382fd307d9b7ed683e4c10954d44ae7c6 Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Fri, 24 Nov 2017 17:54:44 -0600 Subject: [PATCH 3/6] Remove lambda in test --- test/general/openpgp.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/general/openpgp.js b/test/general/openpgp.js index eab0d9fe..fb151e09 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1207,7 +1207,9 @@ describe('OpenPGP.js public api tests', function() { data: new Uint8Array([0x01, 0x01, 0x01]), passwords: null }) - .then(() => done(new Error('Error expected.'))) + .then(function () { + done(new Error('Error expected.')); + }) .catch(function(error){ expect(error.innerError).to.exist; done(); From 15e39a51b0c8ad8d098a269b377a883cd4130fc2 Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Fri, 24 Nov 2017 19:56:15 -0600 Subject: [PATCH 4/6] Minor whitespace changes --- test/general/openpgp.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/general/openpgp.js b/test/general/openpgp.js index fb151e09..36199f97 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1207,10 +1207,10 @@ describe('OpenPGP.js public api tests', function() { data: new Uint8Array([0x01, 0x01, 0x01]), passwords: null }) - .then(function () { + .then(function() { done(new Error('Error expected.')); }) - .catch(function(error){ + .catch(function(error) { expect(error.innerError).to.exist; done(); }); From a5a913419b88c9b3449507f42e1f90c8af9ece1f Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Sat, 25 Nov 2017 10:45:58 -0600 Subject: [PATCH 5/6] Also concat stacks --- src/openpgp.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/openpgp.js b/src/openpgp.js index e2f9bd5d..7dcb6244 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -553,6 +553,7 @@ function onError(message, error) { // rethrow new high level error for api users const newError = new Error(message + ': ' + error.message); newError.innerError = error; + newError.stack += '\n' + error.stack; throw newError; } From 589fbb19d6b1f486a0da2206b7fa052e9a5a00f3 Mon Sep 17 00:00:00 2001 From: Justin Chase Date: Sat, 25 Nov 2017 12:24:53 -0600 Subject: [PATCH 6/6] Added a test to verify error message --- test/general/openpgp.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 36199f97..eadf3443 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1202,6 +1202,19 @@ describe('OpenPGP.js public api tests', function() { }); describe('Errors', function() { + it('Errors stack should contain the stack of innerError', function(done) { + openpgp.encrypt({ + data: new Uint8Array([0x01, 0x01, 0x01]), + passwords: null + }) + .then(function() { + done(new Error('Error expected.')); + }) + .catch(function(error) { + expect(error.stack).to.match(/\nError: No keys or passwords/); + done(); + }); + }); it('Errors should contain innerError', function(done) { openpgp.encrypt({ data: new Uint8Array([0x01, 0x01, 0x01]),