right now I am trying to make sure I am handling errors correctly in a stream that first gets the id of a group, and then uses that group id to get profile information.
I need it so it's obvious which step is causing the error -- either the get groupId or the get profile info.
Right now I have something like this, but I am unsure if this is correct.
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;
}
);
You are doing it correctly.
Errors from both methods would end up in (error) => {...} method.
Small tests to play with:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
Returns: 'E', 'F', 'G', 'E', 'F', 'G'
With second method throws:
of('A', 'B', 'C').pipe(mergeMap(letter => {
return throwError('bad');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
Returns: "bad"
With first method throws:
throwError('bad').pipe(mergeMap(letter => {
return of('E', 'F', 'G');
})).subscribe(
(res) => {
console.log(res);
},
(error) => {
console.log(error);
}
);
Returns: "bad"