driverdan / node-XMLHttpRequest

XMLHttpRequest for node.js
http://thedanexperiment.com/2009/10/04/emulating-xmlhttprequest-in-node-js/
MIT License
416 stars 299 forks source link

onerror function does not receive an error as an argument #162

Open uladkasach opened 6 years ago

uladkasach commented 6 years ago

This issue can be recreated quite simply:

        return new Promise((resolve, reject)=>{
            var xhr = new XMLHttpRequest();
            //xhr.overrideMimeType("application/json");
            xhr.open("GET", "test", true);
            xhr.onload = function(){
                console.log(this.responseText);
                resolve(this.responseText);
            };
            xhr.onerror = function(error){
                console.log(this);
                throw (error);
            };
            xhr.send();
        })

Although the onerror function is triggered, the error object is undefined.

This is strange since the output of console.log(this) in the above code snippet demonstrates that the reason for the error is clearly known. The output is the following:

{ UNSENT: 0,
  OPENED: 1,
  HEADERS_RECEIVED: 2,
  LOADING: 3,
  DONE: 4,
  readyState: 4,
  onreadystatechange: null,
  responseText: 'Error: connect ECONNREFUSED 127.0.0.1:80\n    at Object.exports._errnoException (util.js:1022:11)\n    at exports._exceptionWithHostPort (util.js:1045:20)\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:14)',
  responseXML: '',
  status: 0,
  statusText: 
   { Error: connect ECONNREFUSED 127.0.0.1:80
       at Object.exports._errnoException (util.js:1022:11)
       at exports._exceptionWithHostPort (util.js:1045:20)
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:14)
     code: 'ECONNREFUSED',
     errno: 'ECONNREFUSED',
     syscall: 'connect',
     address: '127.0.0.1',
     port: 80 },
  withCredentials: false,
  open: [Function],
  setDisableHeaderCheck: [Function],
  setRequestHeader: [Function],
  getResponseHeader: [Function],
  getAllResponseHeaders: [Function],
  getRequestHeader: [Function],
  send: [Function],
  handleError: [Function],
  abort: [Function],
  addEventListener: [Function],
  removeEventListener: [Function],
  dispatchEvent: [Function],
  onload: [Function],
  onerror: [Function] }