RxJS observables 在没有错误回调的情况下捕获错误

RxJS observables catch errors without the error callback

我想知道是否有一种方法能够在没有错误回调的情况下捕获可观察对象的订阅错误

例如,您捕获了这样的错误

 .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);
 }, 

但是如果你看上面的评论,这就是不包括回调时会发生的情况。

忘记把它放进去意味着它 'bubbles' 起来所以我们不能抓住它并用它做任何事情。

更明确地说,即使在方法周围放置 try-catch 也不会捕获错误。

我想知道这个,因为,如果由于某种原因,开发人员忘记放入 catch 回调,有什么办法可以捕获它?因为如果不是,则无法处理错误。

我认为问题在于可观察对象是一个异步进程,它发生在一个新的 thread/task。

谢谢

如果在 observable 中使用 catchError,则可以避免需要错误回调。在流本身而不是订阅时处理错误是很常见的。

类似

source.pipe(
        catchError(err => {
          console.error(err);
        }).subscribe(...)