Skip to content

Commit

Permalink
Properly handle API errors with unknown error types (#1357)
Browse files Browse the repository at this point in the history
  • Loading branch information
praboud-stripe authored Feb 26, 2022
1 parent 57736db commit 2a1a35b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class StripeError extends Error {
case 'invalid_grant':
return new StripeInvalidGrantError(rawStripeError);
default:
return new GenericError('Generic', 'Unknown Error');
return new StripeUnknownError(rawStripeError);
}
}
}
Expand Down Expand Up @@ -122,6 +122,11 @@ class StripeIdempotencyError extends StripeError {}
*/
class StripeInvalidGrantError extends StripeError {}

/**
* Any other error from Stripe not specifically captured above
*/
class StripeUnknownError extends StripeError {}

module.exports.generate = StripeError.generate;
module.exports.StripeError = StripeError;
module.exports.StripeCardError = StripeCardError;
Expand All @@ -134,3 +139,4 @@ module.exports.StripeConnectionError = StripeConnectionError;
module.exports.StripeSignatureVerificationError = StripeSignatureVerificationError;
module.exports.StripeIdempotencyError = StripeIdempotencyError;
module.exports.StripeInvalidGrantError = StripeInvalidGrantError;
module.exports.StripeUnknownError = StripeUnknownError;
3 changes: 3 additions & 0 deletions test/Error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe('Error', () => {
expect(
Error.StripeError.generate({type: 'idempotency_error'})
).to.be.instanceOf(Error.StripeIdempotencyError);
expect(
Error.StripeError.generate({type: 'weird_error'})
).to.be.instanceOf(Error.StripeUnknownError);
});

it('copies whitelisted properties', () => {
Expand Down

0 comments on commit 2a1a35b

Please sign in to comment.