I'm working on an Angular (v18) SPA app. I have an API service with a get call:
public get(route: string, params?: HttpParams): Observable<any> {
const headers = {
method: 'GET'
};
return this.http.get(this.baseUrl + route, {
headers,
params,
observe: 'response'
}).pipe(
map((res: HttpResponse<Object>) => {
// do stuff
}),
catchError((err) => {
console.log("error in api service get");
return throwError(() => err);
})
);
}
Then, there is a Foo service that has a number of functions that call this get method to get various resources. They are all structured like so:
public getFoo1(): Observable<any[]> {
return new Observable((observer) => {
if (this._foo1) {
observer.next(this._foo1);
return observer.complete();
}
this.fetchFoo1()
.pipe(
catchError((err) => {
console.log('Error in getFoo1'); // error handling ends here
return throwError(() => err);
})
)
.subscribe((res) => {
this._foo1 = res;
observer.next(this._foo1);
observer.complete();
});
});
}
private fetchFoo1(nationalStandardId: number): Observable<Foo[]> {
return this.api.get(`/foo`).pipe(
map(res => res.foo),
catchError(err => throwError(() => err))
);
}
Note that I'm attempting to catch and rethrow any errors from the get call. I call these methods in parallel via forkJoin:
const dataCalls = forkJoin({
getFoo1: this.fooService.getFoo1(),
getFoo2: this.fooService.getFoo2(),
getFoo3: this.fooService.getFoo3()
}).pipe(
catchError(err => console.log("error")) // This is never called
);
I've been testing error handling by not having my API running so all HTTP calls fail. The catchError operators in the fetchFoo functions execute, but the error handler in the outer catchError on the forkJoin is never called. Even if I put a pipe on the individual observables in the forkJoin, it doesn't work either:
const dataCalls = forkJoin({
getFoo1: this.fooService.getFoo1().pipe(
catchError(err => console.log("error")) // This is never called
),
getFoo2: this.fooService.getFoo2().pipe(
catchError(err => console.log("error")) // This is never called
),
getFoo3: this.fooService.getFoo3().pipe(
catchError(err => console.log("error")) // This is never called
)
});
Any idea what might be going on here?
(Note: This isn't how I'm actually implementing this. I'm having the fetchFoo methods return a fallback value, and that works just fine. I'm just trying to understand rxjs error handling better here)
Issue in Stackblitz: https://stackblitz.com/edit/angular-xps71aav?file=src%2Ffoo-service.service.ts