1

I am wondering if there is a way to able to catch a subscribe error on an observable without the error callback?

For instance, you catch errors like this

 .subscribe({
 next: (obj) => {
    // Placeholder for code
 },

 // At the moment we need to include this error callback to show that an error has happened.
 // Forgetting to put this in means it 'bubbles' up so we cannot catch it and do anything with it.
 error: (e) => {
    this.showError(e.message);
 }, 

But if you look at the comment above, this is what happens when not including the callback.

Forgetting to put this in means it 'bubbles' up so we cannot catch it and do anything with it.

To be more explicit, even putting a try-catch around the method doesn't catch the error either.

I want to know this, because, if for some reason, a developer forgets to put the catch callback in, is there any way it can be caught? Because if not the error cannot be handled.

I think the issue is that as an observable is an async process, which happens on a new thread/task.

Thanks

1 Answer 1

5

If you use catchError within the observable, you can avoid needing the error callback. It's pretty common to handle the error within the stream itself, rather than on subscribe.

Something like

source.pipe(
        catchError(err => {
          console.error(err);
        }).subscribe(...)
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.