prevent HTTP被记录在浏览器控制台错误 [英] Prevent http errors from being logged in browser console

查看:146
本文介绍了prevent HTTP被记录在浏览器控制台错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何燮preSS HTTP请求和响应的错误在我的应用程序,例如,当一个坏的请求到我的服务器做的,我不希望被记录到错误浏览器控制台。我试着重写 $ exceptionHandler的,但这个逻辑适用于所有的异常,除了HTTP错误,那些仍然记录到浏览器中。我还创建了HTTP拦截器,但我已经把我的 responseError 400错误的请求错误C>:

I'm trying to figure out how to suppress http request and response errors in my app, for example when a bad request is made to my server, I don't want the error to be logged into the browser console. I've tried overriding the $exceptionHandler, but that logic works for all exceptions except HTTP errors, those are still logged to the browser. I've also created HTTP interceptors but the global 400 Bad Request error appears before the logic that I've put in my responseError:

'responseError': function (rejection) {
 console.log(rejection);
 return $q.reject();
}

这不燮preSS错误要么,任何想法?

That doesn't suppress the error either, any ideas?

修改

我得到的错误是这样的:
POST HTTP://本地主机:38349 /令牌400(错误请求)

The error I'm getting is this: POST http://localhost:38349/token 400 (Bad Request)

推荐答案

其实角不记录本。

这是XMLHtt prequest对象,这样做(同样的问题,<一个href=\"http://stackoverflow.com/questions/11949095/how-can-i-stop-jquery-ajax-from-logging-failures-to-the-console\">here用jQuery)。我从角提取的逻辑,并提出一个工作独立的例子:

It's the XMLHttpRequest object that doing that (same issue here with JQuery). I extracted the logic from Angular and made a working standalone example:

var method = 'POST';
var url404 = '/tnseiartneiasrt/arsntiearsntiasrntiarnstsie';

var xhr = new window.XMLHttpRequest();
xhr.open(method, url404, true);
xhr.onreadystatechange = function() {
  if (xhr && xhr.readyState == 4) {
    console.log('should have logged an error already :(');
  }
};
xhr.send(null);

正如你所看到的,那就是它记录XHR对象。有可能是触发XHR请求之前,禁止所有的console.log的方式,但我认为这是更好地为

As you can see, it's the xhr object that logs it. There might be a way to disable all console.log before triggering the xhr request but I think it's better to either


  • 重新设计你的错误流回到200在身上带一个内部错误code内嵌状态code

  • 与控制台记录活(普通用户甚至不知道有关控制台反正)

我选择了第二个选项,因为我认为这是比较容易理解,因为它不需要额外的逻辑,当有人读你的code。

I chose the second option because I think it's easier to understand as it doesn't require extra logic when someone reads your code.

希望这有助于:)

这篇关于prevent HTTP被记录在浏览器控制台错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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