2

Given this little snippet of code:

function getPage () {
        var xhttp = new XMLHttpRequest();
        xhttp.addEventListener("error", getFailed);
        xhttp.open("GET", "http://nonexistentserver.com", true);
        xhttp.send();
        }

function getFailed(msg) {
    // I get an ProgressEvent object which doesn't have any text properties?
        }

When it runs, the getFailed() callback does indeed get called, but I can't find any information on how to determine what the error was. When I searched for information, all I could find were HTML errors (like 404) and bug reports about causing an error. How do I obtain information about what the failure was available in the error callback?

Duston
  • 1,601
  • 5
  • 13
  • 24
  • 1
    why don't you use `fetch`? :-D – Paul Fitzgerald Jul 02 '19 at 21:48
  • See https://stackoverflow.com/questions/42721584/catching-an-access-control-allow-origin-error-in-javascript/42723074#42723074. While the browser itself will log a more-detailed error message to the console, you can’t access that message from your code. See https://bugs.chromium.org/p/chromium/issues/detail?id=118096#c5: *“The details of errors of XHR and Fetch API are not exposed to JavaScript for security reasons.”* – sideshowbarker Jul 03 '19 at 00:12

3 Answers3

1

It seems like in your case it's impossible to get the error information. There are some properties which show the request status: XMLHttpRequest.status, XMLHttpRequest.statusText and XMLHttpRequest.responseText. But they all don't work here (only XMLHttpRequest.status shows '0') in this case. Error event of XMLHttpRequest is called when just error occurs. It doesn't send any information about the error. I hope these will help you: XMLHttpRequest and XMLHttpRequest Properties

Andrew Dashkov
  • 301
  • 1
  • 8
0

You can try to use onprogress event and handle the status code, like this:

var xhr = new XMLHttpRequest;
xhr.onprogress = function () {
  console.log(xhr.status); //404 will be printed on console
};

xhr.open('GET', '/noexists');
xhr.send(); 
  • 1
    My question says I already know how to capture 404 errors. I want to capture errors where the server itself is not found (DNS or network errors for example.) – Duston Jul 08 '19 at 13:33
  • Using this way `xhr.status` will return the error code, than you can handle it according `Client Error Responses` https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#Client_error_responses or `Server Error Responses` https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#Server_error_responses – Cleiton Tortato Jul 09 '19 at 14:49
  • 1
    Again, those are errors that are returned by the server. My question involves errors where the server cannot be contacted. – Duston Jul 09 '19 at 16:00
  • Sorry, with `javascript` and `XMLHttpRequest` you will get just error codes by http protocol level, I think you need a protocol near the network level to get it – Cleiton Tortato Jul 09 '19 at 16:18
0

In the getFailed call:

function getFailed(e) {
    console.log('response error:', e.currentTarget.response);
}

The e.currentTarget is the same xhr instance.