4

I am making a get request to a service to check whether a certain key present - if it is, it returns some data, and if not, it returns a 401. I do not have control over the service - but I do not want to display the 401 in the console - that seems unprofessional, since in most cases this key won't exist. Is there a way to catch the 401 so it never prints to the console?

Right now I have the following - I can test whether the status is 401, but I cannot catch it prevent it from printing out to the console. Is this possible? I don't want a global handler on 401s, I just want to catch one for this particular request.

$http.get(url, { withCredentials: true })
        .success(function(data, status, headers, config) {
          result.resolve(data, status, headers, config);
        })
        .error(function(data, status, headers, config) {
          if(status === 401)
          {
            // I can test for a 401 here, but that doesn't seem to help
          }
          result.reject(data);
        });
Nicole Stein
  • 925
  • 1
  • 9
  • 23

2 Answers2

1

You cannot prevent ajax errors to be printed in the console.

One way to avoid that would be to return a regular 200 from the server with a specific content that you would handle as the missing key error client side.

Or you could use console.clear() right after the response, but I don't know if it is supported by all browser and it may not be a lot more "professional".

floribon
  • 19,175
  • 5
  • 54
  • 66
  • I changed my server to return boolean about authentication. Don't know if it is violating http-protocol but works better for route that only checks if there is valid session id in request. – samu Nov 01 '15 at 18:32
0

What you have achieved on catching the 401 seems to be done on the way it should be done [1]. For the question of console log on the error I have to say no much can be done [2]: at least Chrome console automaticly marks the 401 events to console, without your code being involved at all. Something can be hacked [3], but only on your own machine and browser.

So, answer in short is that don't worry, I suppose only coders will use console, normal Internet users on daily surfing won't lurk much into console anyways.

Sources:

[1] Capture HTTP 401 with Angular.js interceptor

[2] Angular avoid Console trace on $http error

[3] Can I prevent the Chrome Developer Tools console from logging image 404 errors?

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99