node-XMLHttpRequest icon indicating copy to clipboard operation
node-XMLHttpRequest copied to clipboard

onerror function does not receive an error as an argument

Open uladkasach opened this issue 7 years ago • 0 comments

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] }

uladkasach avatar Mar 10 '18 05:03 uladkasach