On a service in Angular, in the following api call, what is the way to catch an error:
getData(sourceId: string): Observable <Array<CustomObject>> {
const obs: BehaviorSubject<Array<CustomObject>> = this.servicesObsCache[sourceId];
const options = new RequestOptions({...});
this.http
.get(API_URL + 'sourceId' + '/serviceendpoint', options, true)
.map(res => res.json())
.first()
.subscribe(
body => {
...
obs.next(services);
},
err => {
obs.next(err); // Is this statement allowed on error?
obs.error(err);
}
);
return obs.filter((results) => !!results);
}
I have tried to use obs.next(err);
but I believe it doesn't catches the error when api fails and instead takes it as success.
What should I use so as to allow the method which invokes the getData(<custom_id>)
, catch the error?