[英]How do I handle catching errors in multiple places in Angular stream?
現在我試圖確保我在 stream 中正確處理錯誤,它首先獲取組的 ID,然后使用該組 ID 獲取配置文件信息。
我需要它,所以很明顯哪一步導致了錯誤——獲取 groupId 或獲取配置文件信息。
現在我有這樣的東西,但我不確定這是否正確。
this.groupRepository
.getUserGroup()
.pipe(mergeMap(group) => {
return this.profileRepository.getAllProfiles(group.id)
})
.subscribe(
(res) => {
// doing things in here to set the groups and profiles
},
(error) => {
this.error = error;
}
);
你做得對。
兩種方法的錯誤最終都會出現在(error) => {...}
方法中。
可以玩的小測試:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:'E'、'F'、'G'、'E'、'F'、'G'
使用第二種方法拋出:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return throwError('bad');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:“壞”
使用第一種方法拋出:
throwError('bad').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
返回:“壞”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.