【问题标题】:Nest httpClient calls and tell which one failed?Nest httpClient 调用并告诉哪个失败了?
【发布时间】:2020-05-28 14:30:20
【问题描述】:

我有一个类似于底部的伪代码 sn-p 的组件。

我需要修改onDeployForTesting(),使其在调用myService.deployForTesting() 之前调用myService.save()。我一直在研究嵌套的 Observables 和对 httpClient 的嵌套调用,但我永远无法判断哪个 Observable 失败了(如果有的话)。我需要知道这一点,以便我可以继续设置通知。

问题

我如何嵌套两个(或更多)httpClient 请求,并查看链中的哪个失败(以 RxJS 方式)?

我尝试过的

onSaveAndDeployForTesting() {
    this.myService
        .save(arg1)
        .pipe(
            concatMap(  // also tried: switchMap, mergeMap
                () => this.myService.deployForTesting(arg2),
            ),
        )
        .subscribe(
            console.info,
            console.error,  // this gives a very generic error with no pointer to which Observable actually failed
            console.info,
        );
}

现有代码

class MyClass {
    // other code

    onSave() {
        this.myService.save(arg1)
            .subscribe(
                (data) => {
                    this.notificationService.set({
                        type: NotificationType.SUCCESS,
                        title: 'Success',
                        description: 'Lorem ipsum dolor sit amet',
                    });
                },
                (err) => {
                    if (err.errorCode === 409) {
                        this.notificationService.set({
                            type: NotificationType.WARNING,
                            title: 'Save Conflict',
                            description: 'Lorem ipsum dolor sit amet',
                        });
                    } else {
                        this.notificationService.set({
                            type: NotificationType.ERROR,
                            title: 'Error',
                            description: 'Lorem ipsum dolor sit amet',
                        });
                    }
                },
            );
    }

    onDeployForTesting(arg1) {
        this.myService.deployForTesting(arg1)
            .subscribe(
                (data) => {
                    if (data.status === 207) {
                        this.notificationService.set({
                            type: NotificationType.WARNING,
                            title: 'Unable to deploy configuration',
                            description: 'Lorem ipsum dolor sit amet',
                        });
                    } else {
                        this.notificationService.set({
                            type: NotificationType.SUCCESS,
                            title: 'Success',
                            description: 'Lorem ipsum dolor sit amet',
                        });
                    }
                },
                (err) => {
                    this.notificationService.set({
                        type: NotificationType.ERROR,
                        title: 'Error',
                        description: 'Lorem ipsum dolor sit amet',
                    });
                },
            );
    }
}

class MyService {
    // other code

    save(data) {
        return this.http.put('/my/save/url', data);
    }

    deployForTesting(data) {
        return this.http.post('/my/deploy/url', data);
    }
}

【问题讨论】:

    标签: angular rxjs angular7 rxjs6


    【解决方案1】:

    你可以使用 RxJS catchError。试试下面的

    import { throwError, of, EMPTY } from 'rxjs';
    import { catchError, concatMap } from 'rxjs/operators';
    
    onSaveAndDeployForTesting() {
      this.myService.save(arg1).pipe(
        catchError(saveError => this.handleSaveError(saveError)),
        concatMap(() => this.myService.deployForTesting(arg2).pipe(
          catchError(deployError => this.handleDeployError(deployError))
        ))
      )
      .subscribe(
        console.info,
        console.error,
        console.info,
      );
    }
    
    handleSaveError(error) {
      // handle error from `save()` call
      return EMPTY;      // also `throwError()` or `of()`
    }
    
    handleDeployError(error) {
      // handle error from `deployForTesting()` call
      return EMPTY;      // also `throwError()` or `of()`
    }
    

    记得从catchError返回一个observable。

    【讨论】:

    • 我原以为会有更聪明或更多 RxJS-y 的东西,但效果很好,我自己应该想到的 ?
    • 另外,我知道EMPTY
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2020-08-20
    • 2015-12-29
    • 2011-10-11
    • 2020-02-10
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多