简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM