Boo Yan Jiong
Boo Yan Jiong

Reputation: 2731

Unable to catch error from subscribe Angular RxJs

I follow the latest tutorial, it use pipe, tap and catchError to intercept the result.

I have:

getStatus(): Observable<boolean> {
    return this.http.get<boolean>('/status').pipe(
        tap((returnedStatus: boolean) => console.log(returnedStatus)),
        catchError(console.log('getStatus error'))
    );
}

When an error happen, the catchError works, and the console.log('getStatus error') is executed and a message is printed.

However, when I subscribe, and place another error handler function in the subscribe, the function never executed even if error occurred.

getStatus().subscribe(
    st=> {
        console.log('data received: '+ st);
    },
    st => {
        console.log('yeah, error appear');
    }
)

Whenever data received, the console.log('data received: ' + st) will be executed, even though if an error occurs.

I know it is related to pipe and catchError, everything work well if I remove the pipe. How can I carry forward the error and let it be seen in the subscribe?

Upvotes: 0

Views: 1392

Answers (1)

Joo Beck
Joo Beck

Reputation: 1913

Your catchError catches the error, at that point it no longer will continue down. If you want it to continue to go down to your lower error handling, you need to return an Observable error.

Upvotes: 2

Related Questions