From 04c514ffef079edbb345e4e7985ba074c6783a93 Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Wed, 25 Sep 2019 18:04:44 -0400 Subject: [PATCH 1/3] Add request-specific fields from raw error to top level error --- lib/Error.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/Error.js b/lib/Error.js index 5b07c0b1d5..f3e6966030 100644 --- a/lib/Error.js +++ b/lib/Error.js @@ -36,6 +36,13 @@ class StripeError extends Error { this.requestId = raw.requestId; this.statusCode = raw.statusCode; this.message = raw.message; + + this.charge = raw.decline_code; + this.decline_code = raw.decline_code; + this.payment_intent = raw.payment_intent; + this.payment_method = raw.payment_method; + this.setup_intent = raw.setup_intent; + this.source = raw.source; } /** From 86f26e404330c294868776c9984fa6fa5179e012 Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Wed, 25 Sep 2019 18:16:25 -0400 Subject: [PATCH 2/3] Fix bug and add test --- lib/Error.js | 2 +- test/Error.spec.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Error.js b/lib/Error.js index f3e6966030..d96ce9a164 100644 --- a/lib/Error.js +++ b/lib/Error.js @@ -37,7 +37,7 @@ class StripeError extends Error { this.statusCode = raw.statusCode; this.message = raw.message; - this.charge = raw.decline_code; + this.charge = raw.charge; this.decline_code = raw.decline_code; this.payment_intent = raw.payment_intent; this.payment_method = raw.payment_method; diff --git a/test/Error.spec.js b/test/Error.spec.js index 7b70aa169e..98775fc30b 100644 --- a/test/Error.spec.js +++ b/test/Error.spec.js @@ -60,6 +60,15 @@ describe('Error', () => { ).to.be.instanceOf(Error.StripeIdempotencyError); }); + it('copies whitelisted properties', () => { + const e = new Error.StripeError({ + charge: 'foo', + unknown_prop: 'bar', + }); + expect(e).to.have.property('charge', 'foo'); + expect(e).not.to.have.property('unknown_prop', 'bar'); + }); + it('Pulls in headers', () => { const headers = {'Request-Id': '123'}; const e = Error.StripeError.generate({ From 81521f0c055d8afdff093f9a612c9b5a2aa67f4b Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Wed, 25 Sep 2019 22:01:05 -0400 Subject: [PATCH 3/3] better test case --- test/Error.spec.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Error.spec.js b/test/Error.spec.js index 98775fc30b..e4fb1229a0 100644 --- a/test/Error.spec.js +++ b/test/Error.spec.js @@ -67,6 +67,7 @@ describe('Error', () => { }); expect(e).to.have.property('charge', 'foo'); expect(e).not.to.have.property('unknown_prop', 'bar'); + expect(e).not.to.have.property('decline_code', 'xyzzy'); }); it('Pulls in headers', () => {