d3 / d3-request

A convenient alternative to XMLHttpRequest.
BSD 3-Clause "New" or "Revised" License
110 stars 54 forks source link

Expose XHR status on error #23

Closed aviddiviner closed 7 years ago

aviddiviner commented 7 years ago

Hi.

When I get a 404 response, the "error" event that's fired only includes an XHR ProgressEvent (in Chrome), and nothing else. That would be this code path.

This isn't too helpful, and I was wondering if you could expose the xhr.status or more details of the underlying XHR itself. Perhaps either as an extra part of the error payload, or similarly to how you allow the user access to the XHR on the request.response(fn) method.

I like that I can do

.response(function(xhr) { /* do something with xhr.responseText */ })

So on an error I want to

.errorResponse(function(xhr) { /* check xhr.status or xhr.getAllResponseHeaders() */ })

Thanks! And thanks for the amazing software. You rock!

mbostock commented 7 years ago

You can use error.target on the ProgressEvent passed to the error handler to get back the underlying XMLHttpRequest instance. For example:

d3.text("/file/not/found", function(error, text) {
  if (error) {
    console.log("fail", error.target.status);
  } else {
    console.log("success", text);
  }
});