Access body for failed easyRequest call
The easyRequest dependency.js does not return the body of the request if an error response code is returned from the request. Is there any way to get at the body?
/**
easyRequest
- Provides a layer of abstraction to easily query HTTP endpoints.
- It does all the error handling (bad response, authentication failed, bad json response, etc).
*/
module.exports = function (jobWorker, io, globalConfig){
var request = require('request');
function query_request (options, callback) {
request(options, function(err, response, body){
if (err || !response || response.statusCode != 200) {
var error_msg = (err || (response ? ("bad statusCode: " + response.statusCode) : "bad response")) + " from " + options.url;
callback(error_msg);
}
else {
callback(null, body);
}
});
}
Comments (9)
-
-
reporter PR sent :-)
-
reporter I've created a new pull request: I changed the variable error_msg to errorMsg and now returning response as well. I still attempt to decode the body as a JSON object if there was an error, but ignore any further errors this might create. The response is returned as the third parameter to maintain backwards compatibility.
-
reporter Third (and hopefully final!) pull request created
-
reporter I did the squash but might have gone a step too far, it seems to have pick up your last commit and taken it as mine :-( I think this change does make the intention of the code more obvious though. try { jsonBody = JSON.parse(body); } catch (e) { if (err) { jsonBody = null; } else { callback('invalid json response', null, response); } }
-
reporter After screwing up the squash I've deleted everything and started from scratch, you should have a new pull request waiting for you - this one looks much better :-)
-
reporter I'm really starting to hate JavaScript! Here's the billionth PR for this change :-( I've tested both the HTML and JSON paths and both worked.
https://bitbucket.org/atlassian/atlasboard/pull-requests/62/easyrequest-changes/diff
-
- changed status to resolved
Added in 588435d
-
reporter Is there a nightly build that I can use to get access to this fix on my build server? If not, do you have an estimate of when the next release is due, or can you recommend another way of getting the fix onto my build server?
- Log in to comment
We could return the response as second parameter. Fancy to send a PR? :)