简体   繁体   中英

Catching Error in pipe() method of an Observable - rxjs/Axios

I refactored my http layer to go from a promised-based implementation to observables using rxjs . The problem that I am facing is that the code crashes whenever server response is 400 or 500 ,

Axios.request(config).pipe(map(((response: AxiosResponse) => response), catchError(e => {
    return new Observable(e);
})));

The problem I am facing is that the error is not being handled by the catchError callback. I am looking for a way in which the error is handled by the catchError callback so that the response can be handled gracefully.

I don't know your problem is this but you can do this:

import { EMPTY } from 'rxjs';

Axios.request(config).pipe(map(((response: AxiosResponse) => response), 
catchError(() => EMPTY)));

EMPTY is an object that is imported from rxjs libaray.

It looks like you want to turn the error notification to next notification so you need to use return of(e) eventually EMPTY if you want to suppress the error.

new Observable(e) is probably what throws the error in your case because the parameter passed to Observable needs to be a function which e is not in this case.

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