Access body for failed easyRequest call

Issue #109 resolved
Andrew Sumner created an issue

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)

  1. Andrew Sumner 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.

  2. Andrew Sumner 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); } }

  3. Andrew Sumner 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 :-)

  4. Andrew Sumner 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?

  5. Log in to comment