Skip to content

Commit 0797632

Browse files
mdlavinrattrayalex-stripe
authored andcommitted
Handle errors from the oauth/token endpoint (stripe#602)
1 parent 3da88b7 commit 0797632

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/Error.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ StripeError.generate = function(rawStripeError) {
5959
return new _Error.StripeAPIError(rawStripeError);
6060
case 'idempotency_error':
6161
return new _Error.StripeIdempotencyError(rawStripeError);
62+
case 'invalid_grant':
63+
return new _Error.StripeInvalidGrantError(rawStripeError);
6264
}
6365
return new _Error('Generic', 'Unknown Error');
6466
};
@@ -73,3 +75,4 @@ _Error.StripeRateLimitError = StripeError.extend({type: 'StripeRateLimitError'})
7375
_Error.StripeConnectionError = StripeError.extend({type: 'StripeConnectionError'});
7476
_Error.StripeSignatureVerificationError = StripeError.extend({type: 'StripeSignatureVerificationError'});
7577
_Error.StripeIdempotencyError = StripeError.extend({type: 'StripeIdempotencyError'});
78+
_Error.StripeInvalidGrantError = StripeError.extend({type: 'StripeInvalidGrantError'});

lib/StripeResource.js

+9
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ StripeResource.prototype = {
152152
if (response.error) {
153153
var err;
154154

155+
// Convert OAuth error responses into a standard format
156+
// so that the rest of the error logic can be shared
157+
if (typeof response.error === 'string') {
158+
response.error = {
159+
type: response.error,
160+
message: response.error_description
161+
}
162+
}
163+
155164
response.error.headers = headers;
156165
response.error.statusCode = res.statusCode;
157166
response.error.requestId = res.requestId;

test/StripeResource.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ describe('StripeResource', function() {
230230
});
231231
});
232232

233+
it('should handle OAuth errors gracefully', function (done) {
234+
nock('https://connect.stripe.com')
235+
.post('/oauth/token')
236+
.reply(400, {
237+
error: 'invalid_grant',
238+
error_description: 'This authorization code has already been used. All tokens issued with this code have been revoked.'
239+
});
240+
241+
realStripe.setMaxNetworkRetries(1);
242+
243+
realStripe.oauth.token(options.data, function (err) {
244+
expect(err.type).to.equal('StripeInvalidGrantError');
245+
done();
246+
});
247+
});
248+
233249
it('should retry on a 503 error when the method is POST', function(done) {
234250
nock('https://' + options.host)
235251
.post(options.path, options.params)

0 commit comments

Comments
 (0)