我试图从Angular拦截器捕获http请求的错误,并在注销时重试503和504响应n
次,将401作为注销注销。
这是我的http拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(error => {
if (error.status === 401) {
this.authenticationService.logout();
this.router.navigate(['login']);
}
return throwError(error);
}),
retryWhen(errors => errors
.pipe(
concatMap((error, count) => {
if (count < 2 && (error.status == 503 || error.status == 504)) {
return of(error.status);
}
return throwError(error);
}),
delay(500)
)
)
);
}
我100%确信编写此代码后就可以使用它,因为我多次测试它,但是现在它给了我:
UnsubscriptionErrorImpl
{message: "1 errors occurred during unsubscription:↵1) TypeError: Cannot read property 'apply' of null", name: "UnsubscriptionError", errors: Array(1)}
errors: Array(1)
0: TypeError: Cannot read property 'apply' of null at RetryWhenSubscriber._zoneUnsubscribe (http://localhost:4200/polyfills.js:8506:44) at RetryWhenSubscriber.push../node_modules/rxjs/_esm5/internal/Subscription.js.Subscription.unsubscribe (http://localhost:4200/polyfills.js:3739:30) at RetryWhenSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.unsubscribe (http://localhost:4200/polyfills.js:3524:38) at RetryWhenSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._unsubscribeAndRecycle (http://localhost:4200/polyfills.js:3541:14) at RetryWhenSubscriber.push../node_modules/rxjs/_esm5/internal/operators/retryWhen.js.RetryWhenSubscriber.notifyNext (http://localhost:4200/vendor.js:155632:14) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._next (http://localhost:4200/polyfills.js:2717:21) at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (http://localhost:4200/polyfills.js:3504:18) at InnerSubscriber.rxjs.Subscriber.next (http://localhost:4200/polyfills.js:8537:29) at Notification.push../node_modules/rxjs/_esm5/internal/Notification.js.Notification.observe (http://localhost:4200/polyfills.js:2769:50) at AsyncAction.push../node_modules/rxjs/_esm5/internal/operators/delay.js.DelaySubscriber.dispatch (http://localhost:4200/vendor.js:153337:40)
length: 1
__proto__: Array(0)
message: "1 errors occurred during unsubscription:↵1) TypeError: Cannot read property 'apply' of null"
name: "UnsubscriptionError"
上次运行时,我使用的是角度6。现在我使用的是角度7。 我已经尝试了不同的rxjs版本,但无济于事。
如何解决此问题并实现简单的重试逻辑?
编辑:
我刚刚注意到,如果我从import 'zone.js/dist/zone-patch-rxjs';
文件中删除了poliyfills.ts
,那么它就可以了,但是其他事情就停止了。
答案 0 :(得分:0)
在发生401
的情况下,您需要关闭流,以避免进一步的逻辑。
if (error.status === 401) {
this.authenticationService.logout();
this.router.navigate(['login']);
return EMPTY; // <- add this
}