Angular8 HttpInterceptor返回值

问题描述 投票:0回答:1

我有一个基本的HttpInterceptor,我在其中使用rxjs retryWhen,以便在出现服务故障时重试一定次数。如果服务调用已达到最大重试次数,那么我想将此反馈给最初引发该服务调用的方法。

我的问题是,如何将控制权返回给http呼叫的原始发起者?我需要这样做,以便在单个位置(拦截器)集中处理重试的控制,并且我希望能够在调用函数中回调成功/失败方法。

我的问题是该错误被全局错误处理程序吞没,并且没有任何内容传回给我的调用者。

示例:

this.MyServiceCall()
        .pipe(
          map((result) => {
            console.log('this is called when the service returns success');
          }),
         )
         // If there is an error, then how can I show it?
      })
    }


export class HttpRetryInterceptorService implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
        retryWhen(errors => errors
            .pipe(
            concatMap((err:HttpErrorResponse, count) => iif(
            () => (count < 3),
            of(err).pipe(
                map(()=>{
                    console.log(count.toString())
                }),
                delay((2 + Math.random()) ** count * 200)),
                throwError(err)
            ))
        ))
    );
  }
}
angular rxjs angular8 angular-http-interceptors retrywhen
1个回答
0
投票

尝试使用catchError()

this.MyServiceCall()
        .pipe(
          map((result) => {
            console.log('this is called when the service returns success');
          }),
         )
         catchError((error) => {
               // Do something and return either an observable or rethrow the error
             return throwError(error);
         })
      })
    }

https://www.learnrxjs.io/operators/error_handling/catch.html

© www.soinside.com 2019 - 2024. All rights reserved.