Skip to content

Commit

Permalink
Upgrade gowalla to request and error handling++
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoguchi committed Apr 22, 2012
1 parent a7ffacc commit a602e5e
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions lib/modules/gowalla.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var oauthModule = require('./oauth2')
, rest = require('restler');
, request = require('request');

var gowalla = module.exports =
oauthModule.submodule('gowalla')
Expand All @@ -18,21 +18,45 @@ oauthModule.submodule('gowalla')

.fetchOAuthUser( function (accessToken) {
var promise = this.Promise();
rest.get(this._apiHost + '/users/me', {
query: { oauth_token: accessToken },
headers: {
"X-Gowalla-API-Key": this.appId(),
"Accept": "application/json"
request.get({
url: this._apiHost + '/users/me'
, qs: { oauth_token: accessToken }
, headers: {
"X-Gowalla-API-Key": this.appId()
, "Accept": "application/json"
}
}, function (err, body, res) {
if (err) {
err.extra = {res: res, data: body};
return promise.fail(err);
}
}).on('success', function (data, res) {
promise.fulfill(data);
}).on('error', function (data, res) {
promise.fail(data);
if (parseInt(res.statusCode/100, 10) !== 2) {
return promise.fail({extra: {data: body, res: res}});
}
var oauthUser = JSON.parse(body);
return promise.fulfill(oauthUser);
});

return promise;
})

.convertErr( function (data) {
return new Error(data);
.moduleErrback( function (err, seqValues) {
if (err instanceof Error) {
var next = seqValues.next;
return next(err);
} else if (err.extra) {
var gowallaResponse = err.extra.res
, serverResponse = seqValues.res;
serverResponse.writeHead(
gowallaResponse.statusCode
, gowallaResponse.headers);
serverResponse.end(err.extra.data);
} else if (err.statusCode) {
var serverResponse = seqValues.res;
serverResponse.writeHead(err.statusCode);
serverResponse.end(err.data);
} else {
console.error(err);
throw new Error('Unsupported error type');
}
});

0 comments on commit a602e5e

Please sign in to comment.