Reputation: 13
I'm trying to create a service that handle technical errors but i'm facing an issue. The type of the returned item change if i do a catch:
Observable.of(1, 2, 3).subscribe(
item => {}, // item type is number
error => {}
);
Observable.of(1, 2, 3).catch(error => {
// do something
throw error;
}).subscribe(
item => {}, // item type is number | {}
error => {}
);
Is there a way to keep the item type as only a number when catching an observable ?
Context: I'm trying to share the handle of web services throught multiple service:
Maybe i'm not doing it the good way, any advice/sample would be aprreciated.
Thank
Upvotes: 1
Views: 684
Reputation: 21339
Check this
Remember to return an observable from the catch function
Don't throw an error in the error function, the purpose of the .catch is to provide a fallback value without breaking the stream. this fallback must be another observable (while you will still get the content of the observable on the first argument of the subscribe).
Observable.of(1, 2, 3).catch(error => {
const fallbackValue: number = 42;
Rx.Observable.of(fallbackValue);
}).subscribe(
item => {}, // item type is number
error => {}
);
Upvotes: 1