Skip to content

Commit 1ea5ad0

Browse files
committed
Parse errors from Google+ API.
1 parent 56d64b7 commit 1ea5ad0

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

lib/errors/googleplusapierror.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* `GooglePlusAPIError` error.
3+
*
4+
* References:
5+
* - https://developers.google.com/+/web/api/rest/
6+
*
7+
* @constructor
8+
* @param {string} [message]
9+
* @param {number} [code]
10+
* @access public
11+
*/
12+
function GooglePlusAPIError(message, code) {
13+
Error.call(this);
14+
Error.captureStackTrace(this, arguments.callee);
15+
this.name = 'LiveConnectAPIError';
16+
this.message = message;
17+
this.code = code;
18+
}
19+
20+
// Inherit from `Error`.
21+
GooglePlusAPIError.prototype.__proto__ = Error.prototype;
22+
23+
24+
// Expose constructor.
25+
module.exports = GooglePlusAPIError;

lib/oauth2.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy
55
, util = require('util')
66
, GooglePlusProfile = require('./profile/googleplus')
7-
, InternalOAuthError = require('passport-oauth').InternalOAuthError;
7+
, InternalOAuthError = require('passport-oauth').InternalOAuthError
8+
, GooglePlusAPIError = require('./errors/googleplusapierror');
89

910
var DEPRECATED_SCOPES = {
1011
'https://www.googleapis.com/auth/userinfo.profile': 'profile',
@@ -92,18 +93,15 @@ Strategy.prototype.userProfile = function(accessToken, done) {
9293
var json;
9394

9495
if (err) {
95-
// TODO
96-
/*
9796
if (err.data) {
9897
try {
9998
json = JSON.parse(err.data);
10099
} catch (_) {}
101100
}
102-
101+
103102
if (json && json.error) {
104-
return done(new LiveConnectAPIError(json.error.message, json.error.code));
103+
return done(new GooglePlusAPIError(json.error.message, json.error.code));
105104
}
106-
*/
107105
return done(new InternalOAuthError('Failed to fetch user profile', err));
108106
}
109107

test/strategy.profile.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,38 @@ describe('Strategy#userProfile', function() {
9999
});
100100
});
101101

102+
describe('error caused by invalid token when using Google+ API', function() {
103+
var strategy = new GoogleStrategy({
104+
clientID: 'ABC123',
105+
clientSecret: 'secret',
106+
userProfileURL: 'https://www.googleapis.com/plus/v1/people/me'
107+
}, function() {});
108+
109+
strategy._oauth2.get = function(url, accessToken, callback) {
110+
if (url != 'https://www.googleapis.com/plus/v1/people/me') { return callback(new Error('incorrect url argument')); }
111+
112+
var body = '{\n "error": {\n "errors": [\n {\n "domain": "global",\n "reason": "authError",\n "message": "Invalid Credentials",\n "locationType": "header",\n "location": "Authorization"\n }\n ],\n "code": 401,\n "message": "Invalid Credentials"\n }\n}\n';
113+
callback({ statusCode: 401, data: body });
114+
};
115+
116+
var err, profile;
117+
118+
before(function(done) {
119+
strategy.userProfile('invalid-token', function(e, p) {
120+
err = e;
121+
profile = p;
122+
done();
123+
});
124+
});
125+
126+
it('should error', function() {
127+
expect(err).to.be.an.instanceOf(Error);
128+
expect(err.constructor.name).to.equal('GooglePlusAPIError');
129+
expect(err.message).to.equal("Invalid Credentials");
130+
expect(err.code).to.equal(401);
131+
});
132+
}); // error caused by invalid token
133+
102134
describe('error caused by malformed response', function() {
103135
var strategy = new GoogleStrategy({
104136
clientID: 'ABC123',

0 commit comments

Comments
 (0)