When I make a GET request using the Angular HttpClient, I get an observable back and process it within the RxJS operator mergeMap.
Now it happens that again and again a 404 is thrown, which I would like to catch. Finally, no error message should appear in the browser console and the pipe should be processed with the next value of the stream.
Is there a possibility for that? I did not manage it with catchError().
Here is a simplified version of my code:
...
this.service1.getSomeStuff().pipe(
mergeMap((someStuff) => {
return from(stuff);
}),
mergeMap((stuff) => {
return this.service2.getMoreStuff(stuff.id); // Here I need some error handling, if 404 occurs
}),
mergeMap((things) => {
return from(things).pipe(
mergeMap((thing) => {
if (allLocations.some(x => x.id === metaData.id)) {
return this.service2.getMore(thing.id, thing.type, thing.img_ref);
}
}),
map((thing) => {
...
UPDATE: Added approach with catchError()
I tried it that way, but the error is not detected and the next mergeMap does not work (IDE does not recognize parameter like thing.id, thing.type, thing.img_ref anymore):
...
this.service1.getSomeStuff().pipe(
mergeMap((someStuff) => {
return from(stuff);
}),
mergeMap((stuff) => {
return this.service2.getMoreStuff(stuff.id).pipe(
catchError(val => of(`Error`))
);
}),
mergeMap((things) => {
return from(things).pipe(
mergeMap((thing) => {
if (allLocations.some(x => x.id === metaData.id)) {
return this.service2.getMore(thing.id, thing.type, thing.img_ref);
}
}),
map((thing) => {
...