我有一个基本的 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)
))
))
);
}
}