19

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,

Georges Legros
  • 2,494
  • 2
  • 23
  • 42
  • 1
    Are you sure that's AngularJS and not the browser? I'm quite sure the browser logs every HTTP error. [You can use this as a filter on your console logging though.](http://stackoverflow.com/a/14427545) – Philipp Gayret Jun 13 '14 at 14:05
  • Well, you might be right, the only thin I know is that I get this: http://postimg.org/image/g54vhjmrz/ – Georges Legros Jun 13 '14 at 14:48

2 Answers2

18

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.

Community
  • 1
  • 1
Philipp Gayret
  • 4,870
  • 1
  • 28
  • 34
  • 1
    This isn't a solution if you want to avoid polluting the console for all users, not just yourself. The question is a good one. I want the server to throw an exception in case of an error, and I want the client to handle it without polluting the console. – Kesty May 16 '17 at 08:42
  • While it is true that the browser (Chrome and Opera in my case) will log the "network error", you can hide that by simply checking the "Hide network" checkbox in the console filters. However, as the original question states, the error is also being logged by AngularJS. I too would like that to stop. – Jeffrey A. Gochin May 18 '17 at 16:34
  • @JeffreyA.Gochin this answer is 3 years old. If AngularJS now also logs the network errors by default, could you post a new answer with your solution in it? – Philipp Gayret May 29 '17 at 10:57
3

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.

Al-Mothafar
  • 7,949
  • 7
  • 68
  • 102
  • Thanks for the reply, but as you said, this more a workaround and the API I build needs to stay "clean" for clients to use directly (without the front-end) – Georges Legros Mar 28 '16 at 13:20
  • BTW I would not call that ugly, the serves a purpose and I've seen a very huge amount or services and websites using this technique. For myself, I prefer not to do that and trying to stick to Rest as much as I can... – Georges Legros Mar 28 '16 at 13:23
  • So I am, and I think yes a lot of projects use this "ugly" solution if really it is. And I know its maybe too late answer but I just want to add this for community who looking for answer for this kind of question. – Al-Mothafar Mar 28 '16 at 14:48
  • If you want to avoid polluting the console with errors you have handled in the client, this is the only way I've found to do it. I agree that there should be a more elegant way. – Kesty May 16 '17 at 08:44