Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create centralized exception handling to recover from unhandled exceptions

I'm trying to create centralized exception handling (for all code not just Observables) in Angular 2, so that if there is an unhandled error it won't crash the application, but will log it. At the moment my application becomes unresponsive when there is an unhandled exception and I have to reload it. I've created an errorhandler 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 re-thrown as my application stops working. Please help.

like image 862
LanceM Avatar asked Nov 15 '16 16:11

LanceM


2 Answers

My question was similar to this post.

I think the answer is that there is no way to recover from an unhandled exception apart from reloading the page/application. As Günter Zöchbauer stated:

Generally exception should be handled as close as possible to the cause when there is a way to recover.

like image 152
LanceM Avatar answered Nov 18 '22 15:11

LanceM


Use provide to get your exception handler.

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

like image 34
bhantol Avatar answered Nov 18 '22 15:11

bhantol