ggrossetie / unxhr

Synchronous and asynchronous XMLHttpRequest for Node
MIT License
0 stars 3 forks source link

Must throws error when using synchronous mode #32

Closed ggrossetie closed 3 years ago

ggrossetie commented 3 years ago

Original work by @djencks

In the browser, the following code will throw an exception:

try { 
  const xhr = new XMLHttpRequest();
  xhr.open('GET', `http://nodns`, false); 
  xhr.send();
} catch(e) {
  console.log({e})
}

The error is a DOMException (which is not available in a Node environment) with the following information:

code: 19
message: "Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://nodns/'."
name: "NetworkError"
stack: (...)

I've added a special case to create a similar exception when Node throws a ENOTFOUND from dns.lookup.

ggrossetie commented 3 years ago

In asynchronous mode, onerror is called but as far as I understand we cannot retrieve the cause. The browser (in the developer tools) displays an error: GET http://nodns/ net::ERR_NAME_NOT_RESOLVED.

Both response and responseText are empty.