angular – 如何处理可观察到的错误但继续遵循可观察的错误?
发布时间:2020-12-17 17:46:54 所属栏目:安全 来源:网络整理
导读:我有一个observable,http处理其他observable中间. 如果http get响应代码不是200,我想记下这个错误,但继续下一个observable. 到目前为止我有这个: this.getConfigurationSettings() .do(config = { console.log('configuration settings.: ',config); this.c
我有一个observable,http处理其他observable中间.
如果http get响应代码不是200,我想记下这个错误,但继续下一个observable. 到目前为止我有这个: 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错误代码的observable如下: 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 errors
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); }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |