Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error handling in get and post methods #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions lib/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,16 @@ Graph.prototype.end = function (body) {

Graph.prototype.get = function () {
var self = this;
let callbackCalled = false;

return request.get(this.options, function(err, res, body) {
return request.get(this.options, (err, res, body) => {
if (err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);

handleRequestError(
self,
callbackCalled,
{ message: 'Error processing https request in get', exception: err },
);
callbackCalled = true;
return;
}

Expand All @@ -196,11 +198,13 @@ Graph.prototype.get = function () {
}

self.end(body);
}).on('error', function(err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
}).on('error', (err) => {
handleRequestError(
self,
callbackCalled,
{ message: 'Error processing https request in get', exception: err },
);
callbackCalled = true;
});
};

Expand All @@ -215,28 +219,44 @@ Graph.prototype.post = function() {
, postData = qs.stringify(this.postData);

this.options.body = postData;
let callbackCalled = false;

return request(this.options, function (err, res, body) {
return request(this.options, (err, res, body) => {
if (err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);

handleRequestError(
self,
callbackCalled,
{ message: 'Error processing https request in post', exception: err },
);
callbackCalled = true;
return;
}

self.end(body);
})
.on('error', function(err) {
self.callback({
message: 'Error processing https request'
, exception: err
}, null);
.on('error', (err) => {
handleRequestError(
self,
callbackCalled,
{ message: 'Error processing https request in post', exception: err },
);
callbackCalled = true;
});

};

/**
* Handles request errors
* @param {object} self - the graph object
* @param {boolean} callbackCalled - whether the callback has been called
* @param {object} error - the error object
*/
function handleRequestError(self, callbackCalled, error) {
if (!callbackCalled) {
self.callback(error);
}
}

/**
* Accepts an url an returns facebook
* json data to the callback provided
Expand Down