1

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:

  • WepAPI.Service will handle technical error (404, 500, ...)
  • ComponentRelated.Service will handle functional process and error
  • Component will handle data transformation specific to the UI

Maybe i'm not doing it the good way, any advice/sample would be aprreciated.

Thank

1 Answer 1

1

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 => {}
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.