【问题标题】:Angular2 RxJS 5.01 upgrade TimeoutSubscriber errorAngular2 RxJS 5.01 升级 TimeoutSubscriber 错误
【发布时间】:2017-01-20 17:10:35
【问题描述】:

当我将 RxJS 从 5.0.0-rc.4 升级到 5.0.1 时,我的 HTTP 单元测试失败并显示 ..

超时订阅者

代码适用于 5.0.0-rc.4

这是失败的代码。任何想法表示赞赏。

  private getHttpStream$(emit: any, url: string, httpResponseMapCallback: any, method: string) {
    return this.http[method](url, emit, this.options)
      .timeout(Config.http.timeout, new Error('timeout'))

      .map((response: any) => {
        if (response.status && !response.status.toString().startsWith(2)) {
          return response;
        }
        // within the callback, emit provides access to the data emitted
        return httpResponseMapCallback({emit, response});
      })

      .catch((err: any) => {
        // Different code flow in real code v unit test code
        /* istanbul ignore next */
        if (err.status && err.statusText) {
          return Observable.from([err]);
        }

        return Observable.from([err.message]);
      });
  }

【问题讨论】:

    标签: angular rxjs rxjs5


    【解决方案1】:

    .timeout(timeout, customErr, scheduler) 的重载带有自定义错误has been removed。从 beta/rc 到最终版本是 one of the last changes

    将您的代码更改为:

    return this.http[method](url, emit, this.options)
      .timeout(Config.http.timeout)
    

    或者如果您需要自定义错误:

    return this.http[method](url, emit, this.options)
      .timeout(Config.http.timeout)
      .catch(err => err instanceof Rx.TimeoutError ? Rx.Observable.throw(new MyCustomError()) : Rx.Observable.throw(err))
    

    【讨论】:

    • 我很抱歉,我还没有机会测试这个。我相信你是对的,我会尽快接受这个答案。我从来没有不接受答案,所以期待你尽快+15。谢谢
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2019-03-24
    • 2018-12-16
    • 2017-10-13
    • 2018-05-24
    • 2023-03-07
    相关资源
    最近更新 更多