Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular avoid Console trace on $http error [duplicate]

In my application I am using a REST api to get my data. If a send a request like this

$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.

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

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?

In other words, can I tell angular to not output that crap?

Thanks,

like image 996
Georges Legros Avatar asked Jun 13 '14 13:06

Georges Legros


2 Answers

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:

console sample

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 answer and add a filter to the console.

like image 129
Philipp Gayret Avatar answered Sep 28 '22 11:09

Philipp Gayret


I'm not adding a real "coded" solution for this as Philipp provided the right answer, but I want to post an idea about "workaround" for this.

If you really want to avoid this, the solution simply is that to make all responses with status code greater than or equal 400 as a handled responses (e.g: invalid credentials, email exists, invalid start date, ... and so on) so they all act like a success and the status return with 200, and you can add in the response header or body another property like secondaryStatusCode=400 or errorCode=400, then inside success function in the frontend, you try to read this, in case you have errorCode then it is an error even if status code is 200.

I know this is an ugly solution but you can use this way if you really need to find a workaround for this.

like image 38
Al-Mothafar Avatar answered Sep 28 '22 10:09

Al-Mothafar