AngularJS / rxjs:Observable 未在订阅时捕获错误
AngularJS / rxjs: Observable not catching error at subscription
我正在尝试处理 Observable 上的一些自定义错误。
如果我抛出新的自定义错误,我想退出订阅。
但无论我尝试什么,它仍然会继续进行,不会抛出任何错误。
我试图退出订阅功能并跳到错误处理程序,但我的以下代码没有按预期工作..
const obs = new Observable((observer) => {
observer.next();
}).pipe(
map(() => {
return "Some Data";
}),
tap(() => {
if (true) { // Some condition
return throwError( new Error("Test error") )
}
}),
catchError(() => {
return throwError( new Error("Test error") );
})
)
obs.subscribe((res) => {
console.log("[OBS]", res); // Actual triggered handler
}, err => {
console.log("[OBS]", err); // This handler should be actually triggered
})
A return tap 运算符中的值无效,它不会被进一步操作中的可观察流使用。
如果您使用地图而不是点击,您应该会看到错误消息。
@Loop 的答案是正确的,但是您可以将 iif
运算符与 mergeMap
.
结合使用,而不是 map
iif:
Decides at subscription time which Observable will actually be subscribed.
iif accepts a condition function and two Observables. When an Observable returned by the operator is subscribed, condition function will be called. Based on what boolean it returns at that moment, consumer will subscribe either to the first Observable (if condition was true) or to the second (if condition was false). Condition function may also not return anything - in that case condition will be evaluated as false and second Observable will be subscribed.
所以你可以尝试这样的事情:
const MyCustomError1 = throwError(new Error("Test error 1"));
const MyCustomError2 = throwError(new Error("Test error 2"));
const obs = new Observable(observer => {
observer.next();
}).pipe(
map(() => {
return "Some Data";
}),
mergeMap(v =>
iif(
() => /** Condition goes here **/ true,
/** if true **/ MyCustomError1,
/** if false **/ of(v)
)
),
catchError(() => {
console.log("Error catched");
return MyCustomError2;
})
);
我正在尝试处理 Observable 上的一些自定义错误。
如果我抛出新的自定义错误,我想退出订阅。 但无论我尝试什么,它仍然会继续进行,不会抛出任何错误。
我试图退出订阅功能并跳到错误处理程序,但我的以下代码没有按预期工作..
const obs = new Observable((observer) => {
observer.next();
}).pipe(
map(() => {
return "Some Data";
}),
tap(() => {
if (true) { // Some condition
return throwError( new Error("Test error") )
}
}),
catchError(() => {
return throwError( new Error("Test error") );
})
)
obs.subscribe((res) => {
console.log("[OBS]", res); // Actual triggered handler
}, err => {
console.log("[OBS]", err); // This handler should be actually triggered
})
A return tap 运算符中的值无效,它不会被进一步操作中的可观察流使用。 如果您使用地图而不是点击,您应该会看到错误消息。
@Loop 的答案是正确的,但是您可以将 iif
运算符与 mergeMap
.
map
iif:
Decides at subscription time which Observable will actually be subscribed.
iif accepts a condition function and two Observables. When an Observable returned by the operator is subscribed, condition function will be called. Based on what boolean it returns at that moment, consumer will subscribe either to the first Observable (if condition was true) or to the second (if condition was false). Condition function may also not return anything - in that case condition will be evaluated as false and second Observable will be subscribed.
所以你可以尝试这样的事情:
const MyCustomError1 = throwError(new Error("Test error 1"));
const MyCustomError2 = throwError(new Error("Test error 2"));
const obs = new Observable(observer => {
observer.next();
}).pipe(
map(() => {
return "Some Data";
}),
mergeMap(v =>
iif(
() => /** Condition goes here **/ true,
/** if true **/ MyCustomError1,
/** if false **/ of(v)
)
),
catchError(() => {
console.log("Error catched");
return MyCustomError2;
})
);