我希望能够在调用时处理任何错误this.authService.refreshToken()
.可以在switchmap块中处理错误,或者在这种情况下如何处理错误?
post3(endpoint: string, body: string) : Observable<any> {
if (this.authService.tokenRequiresRefresh()) {
this.authService.tokenIsBeingRefreshed.next(true);
return this.authService.refreshToken().switchMap(
data => {
this.authService.refreshTokenSuccessHandler(data);
if (this.authService.loggedIn()) {
this.authService.tokenIsBeingRefreshed.next(false);
return this.postInternal(endpoint, body);
} else {
this.authService.tokenIsBeingRefreshed.next(false);
this.router.navigate(['/sessiontimeout']);
Observable.throw(data);
}
}
);
}
else {
return this.postInternal(endpoint, body);
}
}
Run Code Online (Sandbox Code Playgroud)
shu*_*son 20
使用catchError方法
this.authService.refreshToken()
.pipe(
switchMap(() => {...}),
catchError((e) => {
// handle e and return a safe value or re-throw
if (isCritical(e)) {
return throwError(e);
}
return of([]);
})
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9863 次 |
最近记录: |