-3

I am new to angular 8. I am calling a node api which sends 401 status code with a message but browser console shows a red color error which I want to prevent from showing it as a red color error in browser console, so how can i do this please help.

2 Answers2

0

that's the browser behaviour toward failed api calls , basically your browser records the calls and if one fails ( not a 200 somthing code ) it will console log it . has nothing to do with angular or your code .

you still can infrom the user in the app about a failed request like authorization failure etc and also prevent warning disabling debug and moving to production . but that wont prevent the request errors from showing.

Still this is a trick to override console.log() (disable or create a custom console) maybe you ll find it useful although its the same as disabling clicks for the user it will limit the informations you give to your users about the site activity .

example of warning on google , its trigger by me blocking a request using the network tab , same if this failed by its own it will display in red ( as an error ) , but this proves that this is a browser behaviour to console log the website activities and certain events .

enter image description here

HijenHEK
  • 1,256
  • 1
  • 4
  • 13
0

If you don't handle non-200 responses by yourself then browser will always log these errors in console. Assuming that you're using Angular's http library to call the API, you can utilize its error callback to handle any 4xx response accordingly.

this.http.get('your-node-api-url').subscribe(
      success_response => {
        console.log('Response: ', success_response);
      },
      error => {
        // catch 4xx error here and do whatever you want to do with it. Below log code 
        // won't show it in red color though.
        // Write your code here to notify the user about error.
        console.log('Error: ', error.message);

      }
    );
Sumit Parakh
  • 1,098
  • 9
  • 19