Skip to content

Commit

Permalink
Merge pull request #21 from Mangopay/bugfix/#20
Browse files Browse the repository at this point in the history
Optim following #20 (Handle authorization error, in order to renew the token)
  • Loading branch information
hobailey authored Aug 31, 2016
2 parents a7a9ee6 + a0e73e0 commit c44ef4d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,18 @@ Api.prototype = {
if (!requestOptions.url) throw new Error('url must be specified when doing a manual request');

request = self.client[method](requestOptions.url, requestOptions, function(data, response){
if (response.statusCode < 200 || response.statusCode > 299) {
if (response.statusCode === 401) {
// Authorization error
self.authorize(function(){
self.method.call(self, method, function(data, response){
// Check if we have to wrap data into a model
if (_.isFunction(callback)) {
callback(data, response);
}
resolve(data, response);
}, options)
});
} else if (response.statusCode < 200 || response.statusCode > 299) {
if (_.isFunction(callback)) callback(data, response);
resolve(data.Message);
self.errorHandler(data.Message, data);
Expand All @@ -165,7 +176,18 @@ Api.prototype = {
});
} else {
request = self.client.methods[method](requestOptions, function(data, response){
if (response.statusCode < 200 || response.statusCode > 299) {
if (response.statusCode === 401) {
// Authorization error
self.authorize(function(){
self.method.call(self, method, function(data, response){
// Check if we have to wrap data into a model
if (_.isFunction(callback)) {
callback(data, response);
}
resolve(data, response);
}, options)
});
} else if (response.statusCode < 200 || response.statusCode > 299) {
if (_.isFunction(callback)) callback(data, response);
resolve(data, response);
self.errorHandler(data.Message, data);
Expand Down

0 comments on commit c44ef4d

Please sign in to comment.