简体   繁体   中英

how to prevent 401 unauthorized error message in browser console in angular

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.

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.

在此处输入图像描述

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);

      }
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM