Angular2 rxjs-开关映射捕捉错误
我希望能够处理调用Angular2 rxjs-开关映射捕捉错误,angular,rxjs,Angular,Rxjs,我希望能够处理调用this.authService.refreshtToken()时出错的任何错误。是否可以在switchmap块中处理错误,或者在这种情况下如何处理错误 post3(endpoint: string, body: string) : Observable<any> { if (this.authService.tokenRequiresRefresh()) { this.authService.tokenIsBeingRefreshed.next
this.authService.refreshtToken()
时出错的任何错误。是否可以在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);
}
}
post3(端点:字符串,主体:字符串):可观察{
if(this.authService.tokenRequiresRefresh()){
this.authService.TokenisBeingRefresh.next(true);
返回此.authService.refreshToken().switchMap(
数据=>{
this.authService.refreshTokenSuccessHandler(数据);
if(this.authService.loggedIn()){
this.authService.TokenisBeingRefresh.next(false);
返回此.postInternal(端点,主体);
}否则{
this.authService.TokenisBeingRefresh.next(false);
this.router.navigate(['/sessiontimeout']);
可观察的投掷(数据);
}
}
);
}
否则{
返回此.postInternal(端点,主体);
}
}
使用该方法
顺便说一句,您需要返回:
returnobservable.throw(数据)代码>此方法是。谢谢@AlexPeters,我更新了示例以使用最新的api。
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([]);
})
);