56

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.

There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.

Is there any way to suppress these messages?

Thanks

Bergi
  • 572,313
  • 128
  • 898
  • 1,281
VLostBoy
  • 3,914
  • 2
  • 22
  • 31
  • 1
    I don't think you can from JavaScript. Yet there might be a (hidden) config in the developer tools – Bergi Oct 21 '12 at 13:41
  • See also [here](https://stackoverflow.com/q/13828211/1048572) and [here](https://stackoverflow.com/q/13044254/1048572) for the same in Firebug. – Bergi Jul 04 '17 at 20:56

2 Answers2

46

This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.

Hiding network messages in Chrome DevTools

See also Filtering the Console output and Additional settings in the devtools documentation.

Bergi
  • 572,313
  • 128
  • 898
  • 1,281
Konrad Dzwinel
  • 35,424
  • 12
  • 95
  • 103
  • 7
    Anybody know if there's a way to do this from your JavaScript code? This won't be the default on downstream machines, asking me why there are "errors" and will I be able to "resolve this problem". – Appurist - Paul W Aug 07 '20 at 18:20
9

Use console.clear() in the catch block or error handler function. It will clear those request error on the console immediately after it is logged.

PLEASE NOTE

From MDN

Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.

Read more about it on MDN

try {
  var req = new XMLHttpRequest();
  req.open('GET', 'https://invalidurl', false);
  req.send();
} catch(e) {
  console.clear();
}

Should log this

GET https://invalidurl/ net::ERR_NAME_NOT_RESOLVED

but it will be cleared.

Abk
  • 1,989
  • 1
  • 21
  • 31
  • 5
    But this would clear everything from the console, not just the 404 error – schoinh Oct 17 '20 at 01:09
  • 1
    @schoinh seems like we can... intercept all console messages... clear the console... and make reality, whatever we want it to be – Ray Foss Dec 18 '20 at 22:30