From bf6c2b93196f9271d50c66e8ac86b7af1d3cc41a Mon Sep 17 00:00:00 2001
From: Justin Chase <justin.m.chase@gmail.com>
Date: Fri, 24 Nov 2017 16:12:37 -0600
Subject: [PATCH] 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();
+          });
+        });
+      })
     }
 
   });