angular - 确定错误属于可观察对象列表的可观察对象
问题描述
const obs1$ = this.service.getAllSources();
const obs2$ = this.service.getWidgetById(1);
combineLatest([obs1$, obs2$])
.subscribe(pair => {
const sources = pair[0];
const widget = pair[1];
// do stuff
}, err => {
// err can be from first or second observable, but which?
if (err.status === 404) {
// here I need to know for which observable the error is ocurred ?!?!
this.utilsService.alert('Widget with id 1 not found');
}
if (err.status === 500) {
// here, I need to know from which observable occured
}
});
从后端我从不发送一个404 status
来获取列表,所以,在这种情况下,我可以确定404
它只来自第二个 observable。但是如果我想在error
方法内部有另一个逻辑,我需要知道它是从哪个 observable 发生的。我怎么能这样做?谢谢
解决方案
您可以将每个可观察catchError
对象分别包装并处理它们:
combineLatest([
obs1$.pipe(catchError(e => ...)),
obs2$.pipe(catchError(e => ...)),
]).subscribe(...)
或者您可以标记错误并在subscribe
. 喜欢
obs1$.pipe(catchError(e => throwError({originalError: e, source: 'whatever you want'}))),
推荐阅读
- bash - Correct test for values in if statement in Bash
- swift - 知道平移手势何时通过视图边界
- excel - 如何使 VBA 命令按钮搜索提供的输入并更改状态(如果存在)?
- d3.js - 格式化日期后尝试在热图上排序日期
- python - 比较分组变量中的两个日期
- maven - 我可以从命令行引用 POM 属性吗?
- bitbucket - Bitbucket Cloud 拉取请求的代码覆盖率
- java - 如何计算像素位置?
- python - 将 Pandas 数据框导出到 Excel 多页文件的正确方法是什么?
- sql - 使用 SQL 访问 DDS 定义的表时省略使用 DB2/400 创建临时访问路径