首页 > 解决方案 > 自定义错误处理程序未捕获 zone.js 中的异常

问题描述

我有一个自定义错误处理程序设置来捕获所有未捕获的异常:

@Injectable()
export class CustomErrorHandler implements ErrorHandler {
  async handleError(err: any) {
    console.log('error: ', err)
  }
}

这似乎有效,除非 Firebase 离线。我在控制台中看到了这个:

index.esm.js:106 [2021-09-23T17:23:48.765Z]  @firebase/firestore: Firestore (8.10.0): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=unavailable]: The operation could not be completed
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

zone-evergreen.js:659 Unhandled Promise rejection: Failed to get document because the client is offline. ; Zone: <root> ; Task: Promise.then ; Value: FirebaseError: Failed to get document because the client is offline.

zone-evergreen.js:364 Uncaught (in promise) FirebaseError: Failed to get document because the client is offline.

我故意阻止网络,以便找到一种方法来捕获此错误并通知客户端。我发现捕获这些错误的唯一方法是将 Firebase 调用包装在try/catch.

这些错误为什么/如何使它超出我的范围CustomErrorHandler?如何在不将每个 Firebase 调用包装成try/catch块的情况下捕获它们?

标签: angularfirebase

解决方案


推荐阅读