Behavior vs Observable Error Handling

Im uses rxJS in Angular and has a set of behavior objects that display as readonly Observable

public _data = new BehaviorSubject<DataItem[]>([]);
public readonly data$ = this._data.asObservable();

Now I noticed that if I subscribe directly to BehaviorSubject, and an error occurs, it will output the error to the console.

but if I subscribe to Observable with the same error, I do not receive any messages and the listener is automatically canceled.

I know this is the expected behavior, but ...

I would like to know what a template is to avoid duplication of code in case of errors, for example.

this.myDataService.data$.subscribe(d=> throwSomeError(), e=> handleError(e));
//or use this:
this.myDataService.data$.subscribe(d=> throwSomeError()).catch(e=> handleError(e));

handleError (e)

+4
source share
1 answer

, BehaviorSubject .

:

?

0

Source: https://habr.com/ru/post/1679338/


All Articles