【发布时间】:2019-02-04 10:39:03
【问题描述】:
我的问题与How to throw error from RxJS map operator (angular) 类似,但我在 angular6 和 rxjs6 上,我想一切都变了 ;)
我想知道,我如何将一个可观察对象映射中的错误对象传播到订阅 OnError 部分。我总是在 OnNext 部分结束。
这是我目前所拥有的:
在一个 ng 组件中,我可能有以下方法调用
[...]
this.dataStreamService.execCall({ method : 'order_list',params : {}})
.subscribe( r => {
// here r provides the result data from http call
console.log("execCall result", r);
}, err => {
// HERE the "MAP ERROR OCCURED" Error should be occured as well,
// but in doesn't
console.log("execCall error",err);
});
[...]
调用的服务方法如下:
execCall(dataStreamCall: DataStreamCall): Observable<DataStreamResult> {
let apiURL = '<some API-URL>';
let params = dataStreamCall.params;
// do HTTP request (this.http calls an extra service handler which wraps
// the angular httpClient and the API errors there
// There is NO Problem with that part :)
let apiResult = this.http.post(apiURL, params);
// Build a new Observable from type "DataStreamResult"
let dsr : Observable<DataStreamResult> = apiResult
.pipe(
map( httpresult => {
if (httpresult['status'] == false){
// the http call was basically successful,
// but state in data is false
// *** THIS IS NOT PROPAGATE TO SUBSCRIBE OnERROR ***
throwError({'msg' : 'MAP ERROR OCCURED'});
// also tried as alternative
return throwError({'msg' : 'MAP ERROR OCCURED'});
} else {
// here the http call was successful
let d = new DataStreamResult();
d.result = httpresult;
return d;
}
}),
catchError( err => {
// error is bubble up from http request handler
return throwError(err);
})
);
return dsr;
}
最后的问题: 如何管理管道“map”中的“throwError”被传播到订阅“err => { ... }”。
实际行为:
throwError({..})
我最终以r = undefined 订阅了 OnNext Part
如果我使用:
return throwError({..})
我还订阅了 OnNext 部分,其中 r 是 throwError-Observable
提前谢谢 最好的问候
【问题讨论】:
标签: error-handling observable angular6 rxjs6