[英]rxjs catchError not working when piped from angular service
使用 redux 和 Angular,我有以下效果:
@Effect()
public authenticate: Observable<Action> = this.actions
.ofType(AUTHENTICATE)
.pipe(
map((action: AuthenticateAction) => action.payload),
switchMap(payload => {
return this.userService.authenticateUser(payload.email, payload.password).pipe(
map(auth => new AuthenticationSuccessAction({ auth: auth })),
catchError(error => of(new HttpErrorAction({ error: error })))
);
})
);
服務:
public authenticateUser(email: string, password: string): Observable<IAuthentication> {
const formData: FormData = new FormData();
formData.append('email', email);
formData.append('password', password);
return this.httpClient.post('/api/auths/useraccounts', formData) as Observable<IAuthentication>;
}
當 POST 失敗時,HttpErrorAction 永遠不會被調度。
結果我忘記了我有一個 HttpInterceptor ,我捕獲了這樣的 http 錯誤:
return next.handle(authReq).pipe(catchError(
(error) => {
return of(error);
}
如果我想將錯誤冒泡到效果,我會執行以下操作:
return Observable.throw(error);
使用較新版本的 rxjs 進行更新:
return throwError(error);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.