从浏览器控制台中删除错误消息(Angular HttpClient) [英] Remove error messages from browser console (Angular HttpClient)

查看:39
本文介绍了从浏览器控制台中删除错误消息(Angular HttpClient)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpClient 有没有办法从浏览器控制台中删除错误消息?

Is there a way with the HttpClient to remove error messages from the browser console?

我当前的代码如下所示:

My current code looks like this:

  getStuff(stuffId: string): Observable<Stuff[]> {
    return this.httpClient.get<Stuff[]>(this.stuff() + stuffId + '/stuff/').pipe(
      catchError((err) => {
        console.log(err.status);
        if (err.status === 404) {
          console.log('Not found');
        }
      })
    );
  }

我在 if 语句中的 console.log('Not found') 被执行,但它仍然向控制台抛出标准错误.

My console.log('Not found') within the if statement is executed, but it still throws the standard error to the console.

我的目标:控制台中没有红色:)

My goal: nothing red in the console :)

更新:

错误不是在 Firefox 中抛出,而是在 Google Chrome 中抛出.为什么..?

The errors are not thrown in Firefox, but in Google Chrome. Why..?

推荐答案

您在控制台中看到的标准错误实际上不是来自代码.由于 API 错误,它来自网络.浏览器会考虑并理解标准的 http 响应代码.因此,每当任何 http 请求返回 2xx 以外的任何响应代码时,它们都会将其视为错误并在控制台中以红色显示.不幸的是,您无法删除它.这是由浏览器完成的.

The standard error you are seeing in console is actually not from code. Its from network, due to API error. The browsers consider and understand standard http response codes. So, whenever any response code other than 2xx is returned by any http request, they consider it an error and show it in red in console. Unfortunately you cannot remove it. That's done by browser.

这篇关于从浏览器控制台中删除错误消息(Angular HttpClient)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆