How to get error information in XmlHttpRequest error event

Viewed 1241

I have simple code like that:

const request = new XMLHttpRequest();
request.addEventListener('error', logError);
request.open('GET', 'www.someapi.com');
request.withCredentials = true;
request.send();

And logging is like that:

const logError = (error) => {
  console.log(`Error\nEvent:\n${JSON.stringify(error, null, 4)}`);
  // Code to log to server
};

Now what happens is that I get log messages with error, but there is like nothing meaningful in there, just "isTrusted": true. I would like to get more information on what is going on and why I get this error. E.g. in the console I see something like that when I fiddle connection drop:

net::ERR_CONNECTION_RESET

But when it comes to javascript event, it's literally no meaningful information to figure out what is going on. E.g. I have read that CORS can cause it here, but without any message it is very hard to figure out what is going on.

1 Answers

You can check the error log as follow :

const request = new XMLHttpRequest();
request.onerror = function (e) {
                console.log(e);
                reject(new TypeError('Network request failed'));
            };
Related