Angular避免$ http错误的控制台跟踪

时间:2014-06-13 13:58:58

标签: javascript angularjs rest

在我的应用程序中,我使用REST api来获取数据。 如果发送这样的请求

$http.get('api/entity/' + $scope.entityId).success(/* DO STUFF */).error(/* DO STUFF */)

在服务中,如果entityId不存在,我返回404。 在错误函数中,我使用状态(第二个参数)捕获它并以适当的方式对其进行操作。

我很生气,因为如果抛出异常并且污染了javascript控制台,则会出现角度。它似乎发生在Angular.js:8165

有没有办法告诉棱角分明我是一个成年的开发人员,我会以一种不错的方式处理他认为是错误的事情?

换句话说,我可以告诉角度不输出那个废话吗?

谢谢,

2 个答案:

答案 0 :(得分:17)

这是您的浏览器功能,而不是执行日志记录的AngularJS的一部分。这是来自此页面控制台的示例:

console sample

您可以看到它记录与您在问题评论中链接的完全相同的错误消息,并且确实是pointing to a part in the source code,但我没有添加任何日志记录语句。

您可以做的最好的事情是关注this answer并向控制台添加过滤器。

答案 1 :(得分:2)

我没有为此添加真正的解决方案,因为Philipp提供了正确的答案,但我想为此发布关于“解决方法”的想法

如果您真的想避免这种情况,那么解决方案就是将所有不成功的响应作为处理的响应(例如:凭据无效,电子邮件存在,无效的开始日期,......和所以,所以他们都行为成功,状态返回200,你可以在响应标题或正文中添加另一个属性,如<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.caneraydin.myapplication.MainActivity"> <ImageView android:layout_width="140dp" android:layout_height="140dp" android:src="@drawable/b3_1" android:layout_alignParentLeft="true" android:layout_centerInParent="true" android:id="@+id/img3" /> <ImageView android:layout_width="140dp" android:layout_height="140dp" android:src="@drawable/b2_5" android:id="@+id/img2" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> <ImageView android:layout_width="140dp" android:layout_height="140dp" android:src="@drawable/b3_2" android:layout_alignRight="@id/img2" android:layout_above="@id/img2" android:id="@+id/img1" android:layout_alignParentBottom="true" /> </RelativeLayout> ,然后在前端内部成功函数中尝试阅读本文,以防万一它不是200。

我知道这是一个丑陋的解决方案,但如果你真的需要为此找到解决方法,你可以使用这种方式。