我该如何处理各种错误,以避免坠机应用程序?

问题描述 投票:0回答:1

我看到了错误处理的几个解决方案。

其主要思路是这样解释的背后:https://angular.io/api/core/ErrorHandler

我试图实现它赶上类型错误,但它不工作:

@Injectable()
export class ErrorsHandler implements ErrorHandler {
  constructor(
     private injector: Injector,
  ) { }

  handleError(error: Error | HttpErrorResponse | TypeError) {
    const notificationService = this.injector.get(NotificationService);
    const dataService = this.injector.get(DataService);
    const router = this.injector.get(Router);

    if (error instanceof HttpErrorResponse) {    
        if (!navigator.onLine) {
            // No Internet connection
            return notificationService.notify('No Internet Connection');
        }
        // Http Error
        // Send the error to the server
        console.log(error);
        // Show notification to the user
        return notificationService.notify(`${error.status} - ${error.message}`);
    } else if (error instanceof TypeError) {
        console.log('This is TypeError!', error);
    }
  }
}
javascript angular
1个回答
0
投票

在这个ELSEIF块检查

err instanceof Error || err instanceof TypeError
© www.soinside.com 2019 - 2024. All rights reserved.