简体   繁体   English

如何在 angular 中的浏览器控制台中防止 401 未经授权的错误消息

[英]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.我是 angular 8 的新手。我正在调用一个节点 api,它发送 401 状态代码和一条消息,但浏览器控制台显示红色错误,我想防止它在浏览器控制台中显示为红色错误,那我怎么能请帮忙。

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.这是浏览器对失败的 api 调用的行为,基本上你的浏览器会记录调用,如果一个失败(不是 200 somthing 代码),它将在控制台记录它。 has nothing to do with angular or your code.与 angular 或您的代码无关。

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.这仍然是覆盖 console.log() (禁用或创建自定义控制台)的技巧,也许您会发现它很有用,尽管它与禁用用户点击相同,它将限制您向用户提供的有关站点活动的信息.

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. google 上的警告示例,它由我触发使用网络选项卡阻止请求,如果它自己失败,它将显示为红色(作为错误),但这证明这是控制台记录网站的浏览器行为活动和某些事件。

在此处输入图像描述

If you don't handle non-200 responses by yourself then browser will always log these errors in console.如果您不自己处理非 200 响应,那么浏览器将始终在控制台中记录这些错误。 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.假设您使用 Angular 的 http 库来调用 API,您可以利用其错误回调来相应地处理任何 4xx 响应。

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

      }
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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