本文介绍了如果出现HTTP错误,如何保证可观察流的连续性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当遇到HTTP错误响应时,以下方法(onTrySignin)有问题.我的HTTP调用之后的catch块可防止Side Effect引发Action错误.如果我console.log出现此错误.

I have a problem with the below method (onTrySignin) when I encounter an HTTP error response. The catch block right after my HTTP call prevents the Side Effect from throwing an Action error. if I do console.log I get this error.

如何保存Observable流并将响应传递到下一个块(mergeMap),在这种情况下我可以触发其他Actions(FailedSignin())?

how can I preserve the Observable stream and pass the response to the next block (mergeMap) where I can fire other Actions, (FailedSignin()) in this case?

onTrySignin = this.actions$
    .ofType(AuthActions.TRY_SIGNIN)
    .map((action: AuthActions.TrySignin) => {
       return action.payload;
      })
    .switchMap((action: DispatchAction) => {
      const trySignInPayload: TrySignInPayload = action.payload;
      return this.httpService.postRequest('Account/Login', (trySignInPayload.loginData))
        .catch((error: any) => {
          console.log(error)
          return Observable.empty();
        })
        .mergeMap((response: HttpResponse<any>) => {
          switch (response.status) {
            case 200:
              if (trySignInPayload.returnUrl) {
                this.router.navigate([trySignInPayload.returnUrl]);
              } else {
                this.router.navigate(['/dbapp']);
              }
              return Observable.concat(
                Observable.of(new AuthActions.GenerateAntiforgeryToken()),
                Observable.of(new AuthActions.Signin(fromAuth.authId, this.fetchUserData()))
              );
              case 401:
              case 404:
              return Observable.concat(
                Observable.of(new AuthActions.FailedSignin()),
                Observable.empty()
              );
            default:
            return Observable.concat(
              Observable.of(new AuthActions.FailedSignin()),
              Observable.empty()
            );
          }
        })
    }).catch((error) => {
      return Observable.throw(error);
    });

这是我的httpService

public postRequest(apiUrl: string, jsonData: {} = {}): Observable<any> {
        return this.httpService.post(this.baseUrl + apiUrl, JSON.stringify(jsonData),
        {observe: 'response', reportProgress: true, withCredentials: true});
    }

推荐答案

您需要创建一个一次性流,这是我们的方法:

You need to create a disposable stream, this is how we do it:

@Effect()
  login$: Observable<Action> = this.actions$
   .ofType(authActions.LOGIN)
   .switchMap((action: any) => {
     // @Effect stream is completing when the error occurs, preventing any further
     // actions. Therefore create a disposable stream to keep @Effect stream alive
     return Observable.of(action)
       .switchMap((action: any) => {
         return this.apiService.login(action.payload);
       })
       .map((x: any) => {
         return new authActions.SetTokensAction({ token: x.data.token });
       })
       .catch((error: any) => {
         return Observable.of(new authActions.LoginFailedAction(error));
       });
    });

这篇关于如果出现HTTP错误,如何保证可观察流的连续性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:12