iriscouch / browser-request

Browser library compatible with Node.js request package
Apache License 2.0
360 stars 102 forks source link

IE11 InvalidErrorStatus #64

Open samuelms1 opened 8 years ago

samuelms1 commented 8 years ago

Using the browser-request module in IE11 causes an InvalidStateError to occur.

The request code looks like this:

var options = {
   url: "....",
   json: true,
   timeout: 2000
};
request.get(options, function(err, res, obj) {
   // Error occurs before here
});

I have a nodejs module that requires the "request" module which on the browser (via browserify) uses the browser-request module. I've mapped this module correlation in my package.json like this:

  "browser": {
    "request": "browser-request"
  }

Note that the request actually appears to work just fine, but IE logs an error

Here is a screenshot of the error in IE11 Developer Tools

image

theopolisme commented 8 years ago

Any updates on this? We're getting these errors too.

samuelms1 commented 8 years ago

I ended up writing a small shim for the request module that fit our very targeted needs (implemented the very minimum that we use from the request module). I would strongly prefer the browser-request module, but to get around this bug we used the shim (note that it assumes jquery is on the page as $).

module.exports = {
    get: function(options, callback){
        $.ajax({
            url: options.url,
            timeout: options.timeout,
            dataType: options.json === true ? 'json' : null,
            error: function(jqXHR, textStatus, errorThrown) {
                var errorMessage = "Error occurred while requesting " + options.url + ".";
                if (textStatus) {
                    errorMessage += "  Error status: " + textStatus;
                    if (errorThrown && errorThrown !== textStatus) {
                        errorMessage += " | Error Thrown: " + errorThrown;
                    }
                }
                var error = new Error(errorMessage);
                if (textStatus === 'timeout') {
                    error.code === 'ETIMEDOUT'
                } 
                error.status = jqXHR.status;
                callback(error);
            },
            success: function(data, textStatus, jqXHR){
                var responseObj = {
                    statusCode: jqXHR.status,
                    body: data
                };
                callback(null, responseObj, data);
            }
        });
    }
};

In package.json I added this:

{
  ...
  "browser": {
    "request": "./lib/browserRequestShim.js"
  }
  ...
}