Angular的HttpInterceptor会屏蔽掉错误信息。
创始人
2024-10-24 05:30:09
0

使用 RxJS 中的 catchError 操作符来捕获错误信息。

示例代码:

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
  
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
    constructor() {}

    intercept(request: HttpRequest, next: HttpHandler): Observable> {
        return next.handle(request).pipe(
            catchError(error => {
                if (error instanceof HttpErrorResponse) {
                    console.error(`Error status code: ${error.status}`);
                } else {
                    console.error(`Error message: ${error.message}`);
                }

                return throwError(error);
            })
        );
    }
}

这个例子中的 HttpInterceptor 会把每个请求都拦截下来,并使用 RxJS 的 catchError 操作符来捕获错误信息。如果错误信息是一个 HttpErrorResponse 对象,它会打印出错误的状态码,否则它会打印出错误信息。无论如何,该方法都会继续抛出错误,以便其他的错误处理器可以继续处理它。

相关内容

热门资讯

透视总结!aapoker怎么提... 透视总结!aapoker怎么提高中牌率(透视)透视方法(总是是真的有挂)1、起透看视 aapoker...
透视辅助!aapoker破解侠... 透视辅助!aapoker破解侠是真的吗(透视)透视脚本(原来是有挂)一、aapoker破解侠是真的吗...
透视软件!aapoker透视方... 透视软件!aapoker透视方法(透视)透视脚本入口(其实是有挂)1、超多福利:超高返利,海量正版游...
透视教程!aapoker怎么拿... 透视教程!aapoker怎么拿好牌(透视)插件下载(总是是有挂);1、完成aapoker怎么拿好牌透...
透视教学!aapoker发牌逻... 透视教学!aapoker发牌逻辑(透视)透视脚本下载(都是有挂)1、金币登录送、破产送、升级送、活动...
透视教学!aapoker怎么设... 透视教学!aapoker怎么设置抽水(透视)辅助(总是真的是有挂)1、金币登录送、破产送、升级送、活...
透视玄学!aapoker怎么开... 透视玄学!aapoker怎么开辅助器(透视)透视方法(竟然是真的有挂)暗藏猫腻,小编详细说明aapo...
透视玄学!aapoker安装包... 透视玄学!aapoker安装包怎么使用(透视)透视插件(果然真的有挂);1、超多福利:超高返利,海量...
透视线上!aapoker怎么提... 透视线上!aapoker怎么提高中牌率(透视)辅助(确实有挂)一、aapoker怎么提高中牌率AI软...
透视辅助!aa poker辅助... 透视辅助!aa poker辅助(透视)辅助(切实真的有挂)该软件可以轻松地帮助玩家将aa poker...