From 49695ff50cfc9743415ced71609f03ba136db774 Mon Sep 17 00:00:00 2001 From: Bart Butler Date: Mon, 27 Nov 2017 15:44:04 -0800 Subject: [PATCH] Error object cannot be used with structured clone, pass stack --- src/openpgp.js | 2 +- src/worker/async_proxy.js | 4 +++- src/worker/worker.js | 2 +- test/general/openpgp.js | 32 ++++++++++++++++---------------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index 7dcb6244..f6dff886 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -552,7 +552,7 @@ 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.innerError = error; + //newError.innerError = error; newError.stack += '\n' + error.stack; throw newError; } diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index a7c0d526..b1857d79 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -66,7 +66,9 @@ AsyncProxy.prototype.onMessage = function(event) { case 'method-return': if (msg.err) { // fail - this.tasks[msg.id].reject(new Error(msg.err)); + const err = new Error(msg.err); + err.stack = msg.stack || err.stack; + this.tasks[msg.id].reject(err); } else { // success this.tasks[msg.id].resolve(msg.data); diff --git a/src/worker/worker.js b/src/worker/worker.js index 62cffaea..ff1e357b 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -86,7 +86,7 @@ function delegate(id, method, options) { // clone packets (for web worker structured cloning algorithm) response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) }); }).catch(function(e) { - response({ id:id, event:'method-return', err:e.message }); + response({ id:id, event:'method-return', err:e.message, stack:e.stack }); }); } diff --git a/test/general/openpgp.js b/test/general/openpgp.js index eadf3443..2cdb5c3c 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1202,8 +1202,8 @@ describe('OpenPGP.js public api tests', function() { }); describe('Errors', function() { - it('Errors stack should contain the stack of innerError', function(done) { - openpgp.encrypt({ + it('Error stack should contain the stack of the original error', function(done) { + return openpgp.encrypt({ data: new Uint8Array([0x01, 0x01, 0x01]), passwords: null }) @@ -1213,22 +1213,22 @@ describe('OpenPGP.js public api tests', function() { .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]), - passwords: null }) - .then(function() { - done(new Error('Error expected.')); - }) - .catch(function(error) { - expect(error.innerError).to.exist; - done(); - }); }); - }) + // it('Errors should contain 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.innerError).to.exist; + // done(); + // }); + // }); + }); } });