LanceM
LanceM

Reputation: 1988

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.

Upvotes: 7

Views: 4471

Answers (2)

LanceM
LanceM

Reputation: 1988

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.

Upvotes: 5

bhantol
bhantol

Reputation: 9616

Use provide to get your exception handler.

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

Upvotes: 1

Related Questions