在chrome dev工具中隐藏401 console.error,在fetch()调用中获取401 [英] Hide 401 console.error in chrome dev tools getting 401 on fetch() call

查看:245
本文介绍了在chrome dev工具中隐藏401 console.error,在fetch()调用中获取401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以进行提取调用。这利用了现代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:未经授权的响应。这是正常的,我希望它被忽略,我可以用代码的流程做。但是,当我尝试运行它时,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 dev过滤器或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).

这里是一个可以解决的样本:

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!");
    });

推荐答案

不幸的是,这不可能做到,因为这种类型的消息在控制台由铬本身打印。压制这类消息多年来一直存在争议,但共识似乎是这个消息是可取的 - 请参阅此讨论

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.

这篇关于在chrome dev工具中隐藏401 console.error,在fetch()调用中获取401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆