我试图创建一个管道运算符,并在未满足特定条件时抛出该异常。但是,我无法抛出并捕获该错误。
这是我的管道:
// My custom pipeable
export function waitFor<T>(thisToComplete$: Observable<any>) {
return (beforeRunningThis$: Observable<T>) => {
return new Observable<T>(observer =>
thisToComplete$.pipe(first()).subscribe(x => {
if (x !== 'Success') {
console.log('Non success result encountered');
return throwError('Rando IO Error');
}
return beforeRunningThis$.subscribe(observer);
})
);
}
}
并使用代码:
const preReq$ = timer(1000);
const dataReq$ = getData();
try {
dataReq$
.pipe(waitFor(preReq$), catchError(x => {
console.log('Code here reached');
return of('Error was handled either here')
}))
.subscribe(x => console.log(`I have result ${x.toString()}`));
} catch (e) {
console.log('Error was handled here');
}
但是,以上控制台均未记录日志。
这是一个stackblitz
最佳答案
由于您正在使用Observable构造observer.error
是你怎么扔的
if (x !== 'Success') {
console.log('Non success result encountered');
observer.error('Rando IO Error');
}
关于angular - 为什么没有将此throwError捕获在catchError中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071648/