Skip to content

Wrapping XMLHttpRequest#open() built in sensitive to order. #453

Closed
@mseeley

Description

@mseeley

Thanks for all the hard work you all do. Our team has enjoyed using Sentry and Raven.

I was finishing an upgrade to 1.3.0 and noticed a 2.0.1 release and took the plunge since we would be able to remove our own built in wrapper plugin.

Reading the Raven implementation it looks like you all are wrapping open(). You're probably aware this requires the consumer to define their event handlers before calling open() while the object's interface doesn't impose order for async requests.

jQuery for example calls open() before defining event handlers. It's not a jQuery-thing at the core, it's just super simple to miss out on Raven's wrapping due to flexibility of the XMLHttpRequest interface.

Our team opted to target send() and wrap the event handler asynchronously. This has seemed to handle any order of calling send(), open() and defining the event handlers. Of course it relies on the request being async.

var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send;
if (_send) {
  global.XMLHttpRequest.prototype.send = function () {
    var self = this;
    setTimeout(function () {
      if (self.readyState !== 4 && self.onreadystatechange) {
        self.onreadystatechange = Raven.wrap(self.onreadystatechange);
      }
    }, 0);
    return _send.apply(this, arguments);
  };
}

Regardless thanks again for the tool. Feel free to nod and close or run with it. Cheers!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions