Rethrowing Errors When Using forkJoin

Discussão em 'Angular' iniciado por MRichards, Maio 15, 2025.

  1. MRichards

    MRichards Guest

    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 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.fetchFoo1(),
    getFoo2: this.fooService.fetchFoo2(),
    getFoo3: this.fooService.fetchFoo3()
    }).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.fetchFoo1().pipe(
    catchError(err => console.log("error")) // This is never called
    ),
    getFoo2: this.fooService.fetchFoo2().pipe(
    catchError(err => console.log("error")) // This is never called
    ),
    getFoo3: this.fooService.fetchFoo3().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)

    Continue reading...

Compartilhe esta Página