问题描述
我有一个服务,负责使用计时器每隔x秒执行一次httpClient.get。 每当服务启动时,我都需要此计时器才能开始运行,因此在服务 构造函数中定义了计时器。根据我的理解,订阅应该在计时器范围内进行注册,如下所示(如果不需要,我不想更改它,除非它不正确。)
只要后端服务器没有error \ exceptions \ error 500异常,则所有系统都可以正常工作。 现在,我需要两件事:
- 只要后端服务器出现问题,我都想 catchError 。
- 我希望观察者能够根据计时器时间(直到下一个滴答声)继续运行,即使有异常也是如此。 每当有异常时,我的最终结果应该是到达组件中的popUpAlert 查看我的代码-这是webapi控制器:
public IActionResult getSomeErrorAstest()
{
try
{
throw new Exception("Serer error");
}
catch(Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError,new List<string>());
//throw ex;
}
}
这是服务(假设每个get请求中的数据都会发生变化-如果确实需要,则无需实施):
export class MyService
{
MyDataSubject = new Subject<any[]>();
MyDataChanged :Observable>any[]> = this.MyDataSubject.asObservable();
subscribe :Subscription;
constructor(private httpClient : HttpClient)
{
this.subscribe = timer(0,30000).pipe(
switchMap(()=>
this.getData())).subscribe();
}
getData()
{
return this.httpClient.get<any[]>(<controller url>)
.pipe(
tap(res =>
{
this.MyDataSubject.next(res);
}),catchError(error =>
{
debugger;//I would expect to catch the debugger here,but nothing happens
return throwError(error);
})
)
}
}
使用者组件:
export class MyComponent (private mySrv : MyService)
{
getMyData()
{
let sub =this.mySrv.MyDataChanged.subscribe(result => doSomething(),error=> popUpAlert());
}
}
解决方法
CatchError
运算符允许处理错误,但不会更改可观察的性质-错误是给定可观察的最终条件,因此发射将停止。CatchError
允许在期望值出现时发出期望值,而不是引发观察者的错误回调(metasong)。
您可能想处理内部Observable中的错误(即,在switchMap
中),因此在那里引发的错误将不会冒泡到主流,这样在发生错误之后,主流将继续,如下所示:
this.subscribe = timer(0,30000)
.pipe(
switchMap(() => this.getData().pipe(catchError(x => of("handle error here"))))
// catchError(...) don't handle error here
)
.subscribe();