Skip to content

Commit

Permalink
Handle errors from the oauth/token endpoint (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlavin authored and rattrayalex-stripe committed May 2, 2019
1 parent c7c1858 commit 6fffb29
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ StripeError.generate = function(rawStripeError) {
return new _Error.StripeAPIError(rawStripeError);
case 'idempotency_error':
return new _Error.StripeIdempotencyError(rawStripeError);
case 'invalid_grant':
return new _Error.StripeInvalidGrantError(rawStripeError);
}
return new _Error('Generic', 'Unknown Error');
};
Expand All @@ -73,3 +75,4 @@ _Error.StripeRateLimitError = StripeError.extend({type: 'StripeRateLimitError'})
_Error.StripeConnectionError = StripeError.extend({type: 'StripeConnectionError'});
_Error.StripeSignatureVerificationError = StripeError.extend({type: 'StripeSignatureVerificationError'});
_Error.StripeIdempotencyError = StripeError.extend({type: 'StripeIdempotencyError'});
_Error.StripeInvalidGrantError = StripeError.extend({type: 'StripeInvalidGrantError'});
9 changes: 9 additions & 0 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ StripeResource.prototype = {
if (response.error) {
var err;

// Convert OAuth error responses into a standard format
// so that the rest of the error logic can be shared
if (typeof response.error === 'string') {
response.error = {
type: response.error,
message: response.error_description
}
}

response.error.headers = headers;
response.error.statusCode = res.statusCode;
response.error.requestId = res.requestId;
Expand Down
16 changes: 16 additions & 0 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ describe('StripeResource', function() {
});
});

it('should handle OAuth errors gracefully', function (done) {
nock('https://connect.stripe.com')
.post('/oauth/token')
.reply(400, {
error: 'invalid_grant',
error_description: 'This authorization code has already been used. All tokens issued with this code have been revoked.'
});

realStripe.setMaxNetworkRetries(1);

realStripe.oauth.token(options.data, function (err) {
expect(err.type).to.equal('StripeInvalidGrantError');
done();
});
});

it('should retry on a 503 error when the method is POST', function(done) {
nock('https://' + options.host)
.post(options.path, options.params)
Expand Down

0 comments on commit 6fffb29

Please sign in to comment.