@@ -107,30 +107,17 @@ function expects (obj, props) {
107107 * @param {Object } data
108108 * @return {String }
109109 */
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
131120 }
132-
133- return Promise . resolve ( data )
134121}
135122
136123/**
@@ -277,13 +264,22 @@ ClientOAuth2.prototype._request = function (options) {
277264
278265 return this . request ( options . method , url , body , options . headers )
279266 . 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+
280274 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 )
285280 }
286- return parseResponseBody ( res . body )
281+
282+ return body
287283 } )
288284}
289285
@@ -378,7 +374,6 @@ ClientOAuth2Token.prototype.refresh = function (options) {
378374 grant_type : 'refresh_token'
379375 }
380376 } , options ) )
381- . then ( handleAuthResponse )
382377 . then ( function ( data ) {
383378 return self . client . createToken ( extend ( self . data , data ) )
384379 } )
@@ -433,7 +428,6 @@ OwnerFlow.prototype.getToken = function (username, password, options) {
433428 grant_type : 'password'
434429 }
435430 } , options ) )
436- . then ( handleAuthResponse )
437431 . then ( function ( data ) {
438432 return self . client . createToken ( data )
439433 } )
@@ -548,7 +542,6 @@ CredentialsFlow.prototype.getToken = function (options) {
548542 grant_type : 'client_credentials'
549543 }
550544 } , options ) )
551- . then ( handleAuthResponse )
552545 . then ( function ( data ) {
553546 return self . client . createToken ( data )
554547 } )
@@ -635,7 +628,6 @@ CodeFlow.prototype.getToken = function (uri, options) {
635628 client_secret : options . clientSecret
636629 }
637630 } , options ) )
638- . then ( handleAuthResponse )
639631 . then ( function ( data ) {
640632 return self . client . createToken ( data )
641633 } )
@@ -686,7 +678,6 @@ JwtBearerFlow.prototype.getToken = function (token, options) {
686678 assertion : token
687679 }
688680 } , options ) )
689- . then ( handleAuthResponse )
690681 . then ( function ( data ) {
691682 return self . client . createToken ( data )
692683 } )
0 commit comments