rxjs Observable.catch()不起作用

时间:2017-05-22 22:36:04

标签: angular rxjs reactive-programming rxjs5

我遇到了一个异常,它正在杀死我的应用程序,我似乎无法捕获()/消耗它。我对此很新,所以我可能会对catch做错了吗?

private callConfigService(): Observable<Map<string,string>> {

    try {
        let confObs = this.contentService.fetchContentUsingGET(id, store, locale, null, false)
            .catch((res) => {
                console.log('Retrieving config FAILED! ', res);    // not hit
                return Observable.from([new Map<string,string>()]);
            }
        ).subscribe(
            (res) => { console.log('fetchContentUsingGET : ', res); },  // not worried about success scenario
            (err) => { console.log('error response: ', err); }  // not hit
         );  
    } catch (exception) {
        console.log('???', exception);  // not hit
    } finally {
        console.log('?????');   // hit
    }
    return Observable.of(new Map<string,string>()); // not hit
}

不幸的是fetchConfigUsingGET来自图书馆,所以我不确定里面是什么。但它看起来像一个热门的观察者,因为它产生了404异常而我甚至没有调用subscribe()

我不明白为什么完全跳过catch()

Uncaught * e {_body: "{"status":404,"error":"StatusCodeError","data":"404 - undefined"}", status: 404, ok: false, statusText: "Not Found", headers: t…}          bundle.umd.js:5

1 个答案:

答案 0 :(得分:0)

你试过这个吗?在订阅运算符中添加OnError处理程序。

let confObs = this.contentService.fetchContentUsingGET(id, store, locale, null, false)
                  .catch((res) => {
                      console.log('Retrieving config FAILED! ', res);
                      return Observable.from([new Map<string,string>()]);
                  })
                  .subscribe(
                      (res) => { console.log('fetchContentUsingGET : ', res);},
                      (err) => { console.log('fetchContentUsingGET.Error : ', err);},
                      () => { //on completed }
                  );