0

我在 Angular 11 中工作,我构建了一个 HttpInterceptor,它的工作是在 http 请求失败时创建自定义错误 toast。

我一直在通过向我的服务器强制发出错误请求来测试拦截器,我没有遇到任何问题,toast 显示正确,但是当我测试 5xx 错误时,只有一些请求被 Angular 的 HttpInterceptor 拦截,尽管我的控制台记录了每个请求的错误. 有谁知道问题出在哪里?

拦截器

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    //sets the header of the intercepted request so it can be logged later
    const headers = req.headers;
    
    //handle responses
    return next.handle(req)
    .pipe(
      catchError((error: HttpErrorResponse) => {
          console.log(req, error)  
          
          if (error instanceof HttpErrorResponse) {
              //shows a toast interface based on the error
          }
     })
}

没有被拦截的请求之一

getPatientRiskFactors(patientId: string): Observable<type> {
    return this.store$.select(selectConfigFile).pipe(
      switchMap(cfg => {
        let url = cfg.configFile.backend.base_url;
        let queryParams = [
        //params
        ];
        return super.http.post<type[]>(url, queryParams, opt)
          .pipe(
            map(val => //array to item))
          );
      })
    )
  }

调用请求

当我初始化这个组件时,我从我的服务器请求多个数据,所以我使用 combineLatest 加入每个调用的 observable。我只能拦截其中一个调用,而其他调用只是由控制台记录。

this.obsElements$ = combineLatest([
      this.patientsService.getPatientDetail(this.patientId)
        .pipe(
          map(val=>val[0])
        ),
      this.patientsService.getPatientAllergyList(this.patientId),
      this.patientsService.getPatientRiskFactors(this.patientId),
      this.patientsService.getPatientsList()
    ]);
4

0 回答 0