Skip to content

Commit 836ecd9

Browse files
author
Eric Koleda
committed
Centralized response parsing and improved error detection. Some APIs return a 200 response with an error message.
1 parent 98da500 commit 836ecd9

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

Service.gs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,7 @@ Service_.prototype.handleCallback = function(callbackRequest) {
299299
},
300300
muteHttpExceptions: true
301301
});
302-
var token = this.parseToken_(response.getContentText());
303-
if (response.getResponseCode() != 200) {
304-
var reason = token.error;
305-
if (!reason) {
306-
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
307-
}
308-
throw 'Error retrieving token: ' + reason;
309-
}
302+
var token = this.getTokenFromResponse_(response);
310303
this.saveToken_(token);
311304
return true;
312305
};
@@ -374,6 +367,24 @@ Service_.prototype.getLastError = function() {
374367
return this.lastError_;
375368
};
376369

370+
/**
371+
* Gets the token from a UrlFetchApp response.
372+
* @param {UrlFetchApp.HTTPResponse} response The response object.
373+
* @return {Object} The parsed token.
374+
* @throws If the token cannot be parsed or the response contained an error.
375+
*/
376+
Service_.prototype.getTokenFromResponse_ = function(response) {
377+
var token = this.parseToken_(response.getContentText());
378+
if (response.getResponseCode() != 200 || token.error) {
379+
var reason = [token.error, token.error_description, token.error_uri].filter(Boolean).join(', ');
380+
if (!reason) {
381+
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
382+
}
383+
throw 'Error retrieving token: ' + reason;
384+
}
385+
return token;
386+
};
387+
377388
/**
378389
* Parses the token using the service's token format.
379390
* @param {string} content The serialized token content.
@@ -432,14 +443,7 @@ Service_.prototype.refresh = function() {
432443
},
433444
muteHttpExceptions: true
434445
});
435-
var newToken = this.parseToken_(response.getContentText());
436-
if (response.getResponseCode() != 200) {
437-
var reason = newToken.error;
438-
if (!reason) {
439-
reason = response.getResponseCode() + ': ' + JSON.stringify(newToken);
440-
}
441-
throw 'Error retrieving token: ' + reason;
442-
}
446+
var newToken = this.getTokenFromResponse_(response);
443447
if (!newToken.refresh_token) {
444448
newToken.refresh_token = token.refresh_token;
445449
}
@@ -541,14 +545,7 @@ Service_.prototype.exchangeJwt_ = function() {
541545
},
542546
muteHttpExceptions: true
543547
});
544-
var token = this.parseToken_(response.getContentText());
545-
if (response.getResponseCode() != 200) {
546-
var reason = token.error;
547-
if (!reason) {
548-
reason = response.getResponseCode() + ': ' + JSON.stringify(token);
549-
}
550-
throw 'Error retrieving token: ' + reason;
551-
}
548+
var token = this.getTokenFromResponse_(response);
552549
this.saveToken_(token);
553550
};
554551

0 commit comments

Comments
 (0)