javascript Angular 避免控制台跟踪 $http 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24207143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular avoid Console trace on $http error
提问by Georges Legros
In my application I am using a REST api to get my data. If a send a request like this
在我的应用程序中,我使用 REST api 来获取我的数据。如果发送这样的请求
$http.get('api/entity/' + $scope.entityId).success(/* DO STUFF */).error(/* DO STUFF */)
In the service, if the entityId does not exist I return a 404. In the error function I catch it using the status (second parameter) and act on it in a proper way.
在服务中,如果 entityId 不存在,我将返回 404。在错误函数中,我使用状态(第二个参数)捕获它并以适当的方式对其进行操作。
I'm being annoyed by the fact that angular if throwing an exception and pollutes the javascript console. It seem to happen on Angular.js:8165
我对 angular 如果抛出异常并污染 javascript 控制台这一事实感到恼火。它似乎发生在 Angular.js:8165 上
It there any way to tell angular that I'm a grown up developer and I will handle what he sees as an error myself in a nice way?
有什么方法可以告诉 angular 我是一个成熟的开发人员,我会以一种很好的方式自己处理他认为的错误吗?
In other words, can I tell angular to not output that crap?
换句话说,我可以告诉 angular 不要输出那些废话吗?
Thanks,
谢谢,
回答by Philipp Gayret
This is your browser's functionality, and not a part of AngularJS that does the logging. Here's a sample from the console of this page:
这是浏览器的功能,而不是进行日志记录的 AngularJS 的一部分。以下是此页面控制台的示例:
You can see it logs the exact same error message as you linked in the question's comment, and is indeed pointing to a part in the source code, but I didn't add any logging statements.
您可以看到它记录了与您在问题评论中链接的完全相同的错误消息,并且确实指向源代码中的一部分,但我没有添加任何日志记录语句。
The best thing you can do is follow this answerand add a filter to the console.
您能做的最好的事情是按照此答案并向控制台添加过滤器。
回答by Al-Mothafar
I'm not adding a real solution for this as Philipp provided the right answer, but I want to post an ideaabout "workaround" for this.
我没有为此添加真正的解决方案,因为 Philipp 提供了正确的答案,但我想为此发布一个关于“解决方法”的想法。
If you really want to avoid this, the solution simply is that to make all not success responses as a handled responses(e.g: invalid credentials, email exists, invalid start date, ... and so on) so they all act as a success and the status return with 200, and you can add in the response header or body another property like secondaryStatusCode=400
, then inside success function in front end you try read this, in case its not 200.
如果您真的想避免这种情况,解决方案只是将所有不成功的响应作为已处理的响应(例如:无效的凭据、电子邮件存在、无效的开始日期等),以便它们都作为成功的响应并且状态返回 200,您可以在响应头或正文中添加另一个属性,例如secondaryStatusCode=400
,然后在前端的成功函数中尝试阅读此内容,以防它不是 200。
I know this is an ugly solution but you can use this way if you really need to find a workaround for this.
我知道这是一个丑陋的解决方案,但如果您确实需要为此找到解决方法,则可以使用这种方式。