7

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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
LanceM
  • 1,888
  • 4
  • 23
  • 41
  • If you implement ErrorHandler you need to override/implement `call` function which could be something like `call(error, stackTrace = null, reason = null)`. Also check the provider part in my answer. – bhantol Nov 15 '16 at 16:39
  • Hi bhantol, Thanks for your answer. I already did have the provide section you showed below. The errors successfully get to the handleError function. The problem is that it still breaks my application. I would like the error to be logged and carry on if possible. – LanceM Nov 15 '16 at 19:25
  • You may want to describe a scenario thus defining what you mean by crash. What happens with the "crash" and what you don;t want to happen is what would help further. – bhantol Nov 15 '16 at 19:28
  • In case of throw you can always catch it and perform what you need to perform in the catch block. You may set $scope.errors and that may show errors to the user. If you want to do this globally then you can have one outer scope where you you can set the error object. – bhantol Nov 15 '16 at 19:41
  • If for a test, I put "throw new Error("hello");" into the constructor of my component, it doesn't render the component. If I put that code in another part of my component then no typescript code events get fired and the page is dead and does nothing. Ideally I would like the errors to be logged and the page rendered and code events fired as normal if possible (log but swallow the unexpected error). I realized sometimes this isn't possible but I would like users to be able to carry on if possible. – LanceM Nov 15 '16 at 19:44
  • I understand I can do try catch and I will do that. I wanted a centralized place for unexpected/unhandled errors so they can be logged and carry on. I am talking about Angular 2 so $scope isn't available. – LanceM Nov 15 '16 at 19:47
  • A component is standalone and each should have its own error handling. However you can share the error handling code with some common handler. Whats is wrong with this angular2 https://plnkr.co/edit/xBgbl5nYU5lGmxBPzTiy?p=preview ? Can you fork it to illustrate ? – bhantol Nov 15 '16 at 20:02

2 Answers2

5

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.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
LanceM
  • 1,888
  • 4
  • 23
  • 41
1

Use provide to get your exception handler.

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

bhantol
  • 9,368
  • 7
  • 44
  • 81