@@ -107,30 +107,17 @@ function expects (obj, props) {
107
107
* @param {Object } data
108
108
* @return {String }
109
109
*/
110
- function getAuthError ( data ) {
111
- var message = ERROR_RESPONSES [ data . error ] ||
112
- data . error ||
113
- data . error_message
114
-
115
- // Return an error instance with the message if it exists.
116
- return message && new Error ( message )
117
- }
118
-
119
- /**
120
- * Handle the authentication response object.
121
- *
122
- * @param {Object } data
123
- * @return {Promise }
124
- */
125
- function handleAuthResponse ( data ) {
126
- var err = getAuthError ( data )
127
-
128
- // If the response contains an error, reject the refresh token.
129
- if ( err ) {
130
- return Promise . reject ( err )
110
+ function getAuthError ( body ) {
111
+ var message = ERROR_RESPONSES [ body . error ] ||
112
+ body . error_description ||
113
+ body . error
114
+
115
+ if ( message ) {
116
+ var err = new Error ( message )
117
+ err . body = body
118
+ err . code = 'EAUTH'
119
+ return err
131
120
}
132
-
133
- return Promise . resolve ( data )
134
121
}
135
122
136
123
/**
@@ -277,13 +264,22 @@ ClientOAuth2.prototype._request = function (options) {
277
264
278
265
return this . request ( options . method , url , body , options . headers )
279
266
. then ( function ( res ) {
267
+ var body = parseResponseBody ( res . body )
268
+ var authErr = getAuthError ( body )
269
+
270
+ if ( authErr ) {
271
+ return Promise . reject ( authErr )
272
+ }
273
+
280
274
if ( res . status < 200 || res . status >= 399 ) {
281
- var err = new Error ( 'HTTP status ' + res . status )
282
- err . status = res . status
283
- err . body = parseResponseBody ( res . body )
284
- return Promise . reject ( err )
275
+ var statusErr = new Error ( 'HTTP status ' + res . status )
276
+ statusErr . status = res . status
277
+ statusErr . body = res . body
278
+ statusErr . code = 'ESTATUS'
279
+ return Promise . reject ( statusErr )
285
280
}
286
- return parseResponseBody ( res . body )
281
+
282
+ return body
287
283
} )
288
284
}
289
285
@@ -378,7 +374,6 @@ ClientOAuth2Token.prototype.refresh = function (options) {
378
374
grant_type : 'refresh_token'
379
375
}
380
376
} , options ) )
381
- . then ( handleAuthResponse )
382
377
. then ( function ( data ) {
383
378
return self . client . createToken ( extend ( self . data , data ) )
384
379
} )
@@ -433,7 +428,6 @@ OwnerFlow.prototype.getToken = function (username, password, options) {
433
428
grant_type : 'password'
434
429
}
435
430
} , options ) )
436
- . then ( handleAuthResponse )
437
431
. then ( function ( data ) {
438
432
return self . client . createToken ( data )
439
433
} )
@@ -548,7 +542,6 @@ CredentialsFlow.prototype.getToken = function (options) {
548
542
grant_type : 'client_credentials'
549
543
}
550
544
} , options ) )
551
- . then ( handleAuthResponse )
552
545
. then ( function ( data ) {
553
546
return self . client . createToken ( data )
554
547
} )
@@ -635,7 +628,6 @@ CodeFlow.prototype.getToken = function (uri, options) {
635
628
client_secret : options . clientSecret
636
629
}
637
630
} , options ) )
638
- . then ( handleAuthResponse )
639
631
. then ( function ( data ) {
640
632
return self . client . createToken ( data )
641
633
} )
@@ -686,7 +678,6 @@ JwtBearerFlow.prototype.getToken = function (token, options) {
686
678
assertion : token
687
679
}
688
680
} , options ) )
689
- . then ( handleAuthResponse )
690
681
. then ( function ( data ) {
691
682
return self . client . createToken ( data )
692
683
} )
0 commit comments