在 chrome 开发工具中隐藏 401 console.error 在 fetch() 调用中得到 401
我有一些代码用于进行 fetch 调用.这利用了现代 chrome/firefox 中内置的 window.fetch api.
I have some code where i make a fetch call. This takes advantage of window.fetch api built into modern chrome / firefox.
代码有时会遇到 401:unauthorized 响应.这是正常的,我希望它被忽略,我可以通过代码流来做到这一点.但是,当我尝试运行 Chrome 时,它会显示难看的 console.error 消息.
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.
如何以编程方式防止此控制台错误显示在所有机器的开发控制台中(即,没有 chrome 开发过滤器或 tampermonkey 类型插件).
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).
这里有一个示例:
fetch("http://httpstat.us/401", {requiredStatus: 'ok'})
.then(function() {
console.log("pass!");
}).catch(function() {
console.log("fail!");
});
推荐答案
很遗憾,这无法做到,因为控制台中的这种类型的消息是由 chrome 自己打印的.压制这种类型的消息已经争论了多年,但共识似乎是这条消息是可取的 - 查看此讨论.
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.
从本质上讲,chrome 的编写方式不允许我们更改此效果,因此我们会收到错误消息.
Essentially, the way chrome was written does not allow us to change this effect, and thus we have the error messages.
相关文章