我有一个基本的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)
))
))
);
}
}发布于 2019-11-18 18:48:54
尝试使用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
发布于 2019-11-18 18:57:13
我认为您可以使用catchError操作符。
您仍然可以在一个地方(即:您的拦截器)处理错误,并通过在您的服务将订阅的可观察的中抛出一个错误,将错误委托给调用方。
注意事项:如果在拦截器中使用throwError运算符,TS应该会对此提出抱怨:
intercept (req: HttpRequest<any>, next: HttpHandler) {
//~~~~~~
const foo = false;
return next.handle(req)
.pipe(
map(ev => foo ? ev : throwError('err!')),
)
}错误是:
类型可观察<从不>不能分配到键入'HttpEvent‘。
HttpEvent类型如下所示:
export type HttpEvent<T> =
HttpSentEvent | HttpHeaderResponse | HttpResponse<T>| HttpProgressEvent | HttpUserEvent<T>;所以,不允许出现错误..。但我在this SO post中找到了一个解决办法。
intercept (req: HttpRequest<any>, next: HttpHandler) {
const foo = false;
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse && !foo) {
throw new HttpErrorResponse({
error: 'err'
});
}
return e;
})
)
}现在,应该在服务的catchError回调中捕获委托错误。
this.MyServiceCall()
.pipe(
map((result) => {
console.log('this is called when the service returns success');
}),
catchError(err => {
// Do something with this error...
})
)编辑
从拦截器抛出错误也可以通过这种方法实现:
intercept (req: HttpRequest<any>, next: HttpHandler) {
const foo = false;
return next.handle(req)
.pipe(
mergeMap(e => {
if (!foo) {
return throwError('err');
}
return of(e);
}),
)
}我错过了这样一个事实:throwError返回了一个可观察的:D。
https://stackoverflow.com/questions/58919538
复制相似问题