0

So for over three days, I'm keep searching on how to get this error hidden from the console and respond to this error:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

Error image in the Google Chrome's console, that I want to hide and handle

Neither official chrome documentation helps. https://developers.google.com/web/tools/chrome-devtools/console/track-exceptions

Nor any tricks that I came along on the google search.

The JavaScript that produces this ineventable error to occur

(For quick testing of the handling solution)

<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
information: <div id="information" ></div>

<script>
 var httpRequest = new XMLHttpRequest();
 httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
 httpRequest.onload = function (e) {
  if (httpRequest.readyState === 4) {
   if (httpRequest.status === 200) {
    console.log(httpRequest.responseText);
   } else {
    document.getElementById("information").innerHTML = "Error Unresponsive Domain";
   }
  }
 };

 httpRequest.send(null);
</script>
</body>
</html>
user3789797
  • 450
  • 6
  • 15
  • What do you mean by respond to error? Are you trying to solve it? It's just literally telling you that this file `https://sdajfkljsdfk.lt` does not exist. It can't pull it from anywhere. Replace that with anything else (ex: `https://code.jquery.com/jquery-3.2.1.min.js`) it won't give you that error. – JeanPaul98 Dec 13 '17 at 15:56
  • That error shows, that this domain is inaccessible, I want that after that error occurs - I could do some changes to the DOM, like: adding text to some part of the page that the domain is not responsive. – user3789797 Dec 13 '17 at 16:08
  • It is impossible to hide the network error that appears in the console other than by simply not having a network error. – Kevin B Dec 13 '17 at 17:35

3 Answers3

1

It is now Oct 2018 and still no great solution for this problem.

Here is an active thread in a Chromium forum, last posts were from this month.

https://bugs.chromium.org/p/chromium/issues/detail?id=124534

Apparently many users do have the claim that 4xx response codes are expected and valid, and should be at least "catch-able" by some means.

halfer
  • 19,824
  • 17
  • 99
  • 186
santiago arizti
  • 4,175
  • 3
  • 37
  • 50
-1

You can't. The reason for network related errors is not exposed to JS. They are hidden just like Same Origin Policy violations.

(If it were possible, then a website could loop over a list of names used on private LANs (such as names used by Intranet servers and IoT devices), detect the errors and identify what devices were on the LAN. This information could then be used to launch further attacks.)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Could you share some related links on this topic and how did chrome's developers decided to use this short of "common sense" solution. That would be helpful to have some sources on this. It's simple, but also interesting to know more about this descision. – user3789797 Dec 13 '17 at 15:11
-1

Try doing this

httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
httpRequest.onload = function (e) {
    if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
            console.log(httpRequest.responseText);
        } else {
            document.getElementById("information").innerHTML = "Error Unresponsive Domain";
        }
    }
};
httpRequest.onerror = function (e) {
    document.getElementById("information").innerHTML = "Error Unresponsive Domain";
};
httpRequest.send(null);

Also you can take a look at this link if my answer is not clear enough. This one too

UPDATE

You're not going to be able to delete one specific error from the console. Only way to clear it is to try something like this:

httpRequest.onerror = function (e) {
    document.getElementById("information").innerHTML = "Error Unresponsive Domain";
    console.clear(); //clear console
};
httpRequest.send(null);

I also stumbled upon this but I don't know if it will help you.

JeanPaul98
  • 492
  • 6
  • 18
  • This actually really clear usage of object.onerror event listener. Thank you very much for that. However, the error still stays in the console even through I handled it using onerror listener. One of the quick solutions came into my head: is to use console.clear(); method. I succeeded in geting rid of that error by clearing everything in the console. But I don't want that the console had that "Console was cleared." showed up. Also I don't want that everything would be cleared up, only this single error that I handled. – user3789797 Dec 13 '17 at 16:38
  • I don't see how Delete command could be relevant to clearing that unnecessary "GET https://sdajfkljsdfk.lt/ net::ERR_NAME_NOT_RESOLVED" error from the console log. It is not like you can delete onerror method and the error will be gone. I was having a though, that maybe exporting console log, filtering it and then importing might be good idea. However I have little doubt that importing console log might be impossible. – user3789797 Dec 13 '17 at 17:47
  • You can't delete one specific error from the log, only way to do it is to clear the whole log see updated answer @user3789797 – JeanPaul98 Dec 13 '17 at 17:49
  • That's for sure, and that's why it is getting harder and harder to solve this solution fully. I do think that there might be a hard and very resources consuming way to do this. I very doubt it's worth it. However partly solution you suggested on this post earlier is great, at least i can respond to the error right now. – user3789797 Dec 13 '17 at 17:53
  • But that error message in the console, might cause a lot of trouble. As it is not needed, but some developer months from now, will wonder why there is this error, editing such code, and it will turn out that the error is useless and uncessary. However, the time will be already spent on solving and checking that error. – user3789797 Dec 13 '17 at 17:55
  • On the latest update, it is exactly what I come up with after you showed how onerror method works on a object. Try and Catch seems to not respond to the httpRequest.send(null); error. That would be a wonder if that worked, and probably, this thread wouldn't exist. – user3789797 Dec 13 '17 at 18:25