Angular 为可观测项列表确定错误所属的可观测项
从后端,我从不发送Angular 为可观测项列表确定错误所属的可观测项,angular,typescript,error-handling,rxjs,observable,Angular,Typescript,Error Handling,Rxjs,Observable,从后端,我从不发送404状态来获取列表,因此,在本例中,我可以确定404仅来自第二个可观察的。但如果我想在error方法中有另一个逻辑,我需要知道它是从哪个可观察对象发生的。我怎么能做到?谢谢您可以将每个观察值包装在catchError中,并分别对它们进行处理: const obs1$ = this.service.getAllSources(); const obs2$ = this.service.getWidgetById(1); combineLatest([obs1$, obs2$]
404状态
来获取列表,因此,在本例中,我可以确定404
仅来自第二个可观察的。但如果我想在error
方法中有另一个逻辑,我需要知道它是从哪个可观察对象发生的。我怎么能做到?谢谢您可以将每个观察值包装在catchError
中,并分别对它们进行处理:
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
}
});
或者您可以标记错误并在订阅中处理它们。类smth
combineLatest([
obs1$.pipe(catchError(e => ...)),
obs2$.pipe(catchError(e => ...)),
]).subscribe(...)
您可以在catchError
中包装每个可观察到的对象,并分别对它们进行处理:
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
}
});
或者您可以标记错误并在订阅中处理它们。类smth
combineLatest([
obs1$.pipe(catchError(e => ...)),
obs2$.pipe(catchError(e => ...)),
]).subscribe(...)
那是个好主意。谢谢这是个好主意。谢谢