[英]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
.我使用rxjs
将我的 http 层重构为 go,从基于承诺的实现到可观察对象。 The problem that I am facing is that the code crashes whenever server response is 400
or 500
,我面临的问题是,每当服务器响应为400
或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.我面临的问题是错误没有被catchError
回调处理。 I am looking for a way in which the error is handled by the catchError
callback so that the response can be handled gracefully.我正在寻找一种由catchError
回调处理错误的方法,以便可以优雅地处理响应。
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.EMPTY
是从 rxjs libaray 导入的 object。
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.看起来您想将error
通知转为next
通知,因此如果您想抑制错误,则需要使用return of(e)
eventually EMPTY
。
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. new Observable(e)
可能是在您的情况下引发错误的原因,因为传递给Observable
的参数需要是 function 而e
在这种情况下不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.