Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I think catch may be a requirement #64

Open
benlesh opened this issue Sep 15, 2023 · 0 comments · May be fixed by #87
Open

I think catch may be a requirement #64

benlesh opened this issue Sep 15, 2023 · 0 comments · May be fixed by #87

Comments

@benlesh
Copy link
Collaborator

benlesh commented Sep 15, 2023

We currently aren't providing a way to gracefully handle an error. We do have error handling in the subscription/observer itself, but no way to have the subscription "recover" from a fatal error.

Since subscriptions have ended the moment an error is emitted from an observable, the catch method behaves a lot like catch on promise, meaning that it returns an observable that will switch to whatever observable you map to in its handler in the event of an error.

For example:

// Given this observable that will always error:
const source = button1.on('click').map((e, i) => {
  if (i === 2) {
    throw new Error('boom!');
  }
  return e;
});


// If we catch the error and return a different observable,
// the new observable takes over the subscription.
source.catch((err) => {
  console.error(err);
  // let another button take over
  return button2.on('click');
})
.subscribe({
  next: console.log,
  error: () => { /* never called */ }
});

// If we catch the error and return the same observable, you can
// resume or "retry" that observable
source.catch((err) => {
  console.log(err);
  return source;
})
.subscribe({
  next: console.log,
  error: () => { /* never called */ }
});

// If we catch the error and throw it again, or throw any other error
// The resulting observable still errors
source.catch((err) => {
  throw new Error('Different error!');
})
.subscribe({
  next: console.log,
  error: console.error
});
@domfarolino domfarolino linked a pull request Nov 29, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants