我有一个可观察的对象,在处理其他可观察对象的过程中是否获取了http。在http get响应代码不是200的情况下,我想记下此错误,但继续进行下一个观察。
到目前为止,我有这个:
this.getConfigurationSettings()
.do(config => {
console.log('configuration settings.: ', config);
this.configSettings = config;
this.subscriptionService.setWSAddressProvider('badUrl');
})
.switchMap(config => this.askForWSData())
.do(config =>
console.log('askForWSData' + config))
.switchMap(r => this.processWSData())
.subscribe(
config => {
console.log('start of data processing: ' + config);
},
err => {
// Log errors if any
console.log(err);
},
() => console.log('app exiting'));
可以返回http错误代码的观察值如下:
setWSAddressProvider() : Observable<string[]> {
return this.http.get('badUrl')
.map((res:Response) => {
this.address = res.text();
return [res.text()];
});
// .catch((error:any) =>
// Observable.throw('Server error')
// );
}
在上述情况下,生成400响应代码。我想记录该收益,但继续进行其他观察。怎么做?
您可以catch
用来处理http错误
setWSAddressProvider() : Observable<string[]> {
return this.http.get('badUrl')
.map((res:Response) => {
this.address = res.text();
return [res.text()];
});
.catch((error: Response | any) => {
if (error instanceof Response) {
if (error.status === 400) {
console.log("Server responded with 400");
// Create a new observable with the data for the rest of the chain
return Observable.of([]);
}
}
// Re-throw unhandled error
return Observable.throw(err);
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句