38

I have some code where i make a fetch call. This takes advantage of window.fetch api built into modern chrome / firefox.

The code sometimes hits a 401:unauthorized response. This is normal and I want it ignored, which I can do with the flow of the code. However, Chrome does show an unsightly console.error message when I try to run it.

How can I PROGRAMMATICALLY prevent this console error from showing in the dev console on all machines (i.e., no chrome dev filters or tampermonkey type plugins).

here's a sample to work off of:

fetch("http://httpstat.us/401", {requiredStatus: 'ok'})
    .then(function() {
        console.log("pass!");
    }).catch(function() {
        console.log("fail!");
    });

enter image description here

Mark Fisher
  • 965
  • 1
  • 11
  • 30
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
  • 2
    according to http://stackoverflow.com/questions/14337351/can-i-prevent-the-chrome-developer-tools-console-from-logging-image-404-errors it may not be possible to do _programmatically_ – szymek Mar 21 '17 at 16:19
  • 1
    thanks I believe that's the case sadly – ThinkBonobo Mar 21 '17 at 18:10
  • You can always use `console.clear()` in the fetch then, catch functions, but that will make you lose anything in the console. Not sure if that is what you want. Any later console statements will work just fine. – gargsms Mar 23 '17 at 19:21
  • 1
    @gargsms I like your ingeniuity :) but in this particular case it sounds like a terrifying hack begging for your code to be unmaintainable. – ThinkBonobo Mar 25 '17 at 00:19
  • Maybe this can help u http://stackoverflow.com/a/43061415/4831179 – blackmiaool Mar 28 '17 at 13:08

1 Answers1

60

Unfortunately, this cannot be done, as this type of message in the console is printed by chrome itself. Repressing this type of message has been debated for years, but the consensus seems to be that this message is desirable - see this discussion.

Just in case you're interested: As per this comment, the reason we're seeing this message is because the response to resource retrieval requests is evaluated, and messages are dispatched at the context level.

Essentially, the way chrome was written does not allow us to change this effect, and thus we have the error messages.

joepin
  • 1,418
  • 1
  • 16
  • 17
  • 3
    For the fun of it, I found the line of code that writes this message: https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp?l=1073 – joepin Mar 23 '17 at 20:12
  • I'd have given it @szymek but your answer is really interesting and also szymek didn't put up an anwer to checkmark :) – ThinkBonobo Mar 25 '17 at 00:21
  • 2
    @joepin link is dead. :( – Jeppe Sep 30 '21 at 20:04