我正在尝试从 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)
)
)
);
}
Run Code Online (Sandbox Code Playgroud)
我 100% 确定这段代码在我编写时有效,因为我对其进行了多次测试,但现在它给了我:
UnsubscriptionErrorImpl
{message: "1 errors occurred during unsubscription:?1) TypeError: …Run Code Online (Sandbox Code Playgroud)