Create centralized exception handling to recover from unhandled exceptions

I am trying to create a centralized exception handling (for all code, not just Observable s) in Angular 2, so that in case of an unhandled error, the application does not hang, but logs it. Currently, my application stops responding when an unhandled exception occurs and I have to reload it. I created an error handler as follows:

 import { NgModule, ErrorHandler } from '@angular/core'; export class AppErrorHandler implements ErrorHandler { rethrowError = false; handleError(error: any) { } } 

Unhandled errors do go to handleError() , but seem to be thrown when my application crashes. Please help.

+7
source share
2 answers

My question was like this post .

I think the answer is that there is no way to recover from an unhandled exception except reloading the page / application. As Gunter Zochbauer said:

Typically, an exception should be handled as close to the reason as possible when there is a recovery method.

+4
source

Use provide to get an exception handler.

providers: [{provide: ErrorHandler, useClass: AppErrorHandler}]

+1
source

Source: https://habr.com/ru/post/1259776/


All Articles