Reputation: 4560
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
Upvotes: 1
Views: 1329
Reputation: 101
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(...)
Upvotes: 5