从 URL 中获取即使在捕获之后也会在控制台中显示错误?

fetching from a URL shows an error in console even after catching it?

假设,我在这个函数中提供了错误的 'uri' 字符串来获取:

function imgChanger(uri) {
    fetch('uri', { mode: 'cors' })//WRONG 'uri' to demonstrate that catch doesn't prevent console's red error
        .then(function (response) {
            if (!response.ok) {
                throw new Error(response.statusText);
            }
            return response.json();
        }).then(function (response) {
            img.src = response.data.images.original.url;
        }).catch((err)=>{
            console.log(err);
        });
}

尽管处理了错误,我还是可以在控制台中看到一条 404 消息:

关于这个我有两个问题:为什么抓到了它却显示出来,我该如何处理?

恐怕您在这里无能为力。 an Issue 124534: Provide a way not to display 404 XHR errors in console 大约 10(!)年前在 Chromium 中打开:

Chromium always displays HTTP 404s as errors in the Javascript console. Apart from forcing line noise onto developers when they may not want it (404s can be spotted easily in the 'Network' tab), I think this actually goes against the spirit of HTTP. If you have a telephone direcotry lookup, for example, and you GET http://myHost/directory/numbers/12345678

... the server should respond with a 404 if that number was not in the directory. This is even really an 'error' in my opinion - it's just information. Why should Chromium display this as an error in the console?

如您所见,它仍然处于打开状态(并且基本上被视为 'by design')。这是另一个有说服力的引述:

8 years gone by and I still cannot control how a client responds to a request. Our in-house API's have been changed to always respond with 200, with a custom status field to correctly filter what is an actual error and what is just simply not there.

It's painfully moronic that we have to move away from REST to actually use a semantic status.

但是很高兴!似乎会在 2021 年展开讨论,所以希望我们最终能让事情有所进展。不幸的是,现在没那么容易了:

There seems to be general agreement that it would be preferable to remove these error messages from the console. But it is also very likely that currently a lot of DevTools users depend on these error messages in their workflow. We have not yet found a way to reconcile these 2 sides.

现在强制性的 xkcd comic: