gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:22:09 25 4
gpt4 key购买 nike

假设,我有这个函数,提供了一个错误的 '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 消息:
enter image description here
我对此有两个问题:为什么尽管捕捉到它仍然显示它,我该如何处理它?

最佳答案

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

Chromium always displays HTTP 404s as errors in the Javascriptconsole. Apart from forcing line noise onto developers when they maynot want it (404s can be spotted easily in the 'Network' tab), I thinkthis actually goes against the spirit of HTTP. If you have atelephone direcotry lookup, for example, and you GEThttp://myHost/directory/numbers/12345678

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


如您所见,它仍然是开放的(本质上被视为“设计”)。这是另一个有说服力的报价:

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

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


但高兴!似乎在 2021 年开始了讨论,所以希望我们最终能取得进展。现在,不幸的是,这并不容易:

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


现在是强制性的 xkcd comic :
enter image description here

关于javascript - 从 URL 获取即使在捕获它之后也会在控制台中显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68053741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com