1

I've searched around quite a bit and haven't found anything that is helping me get over the hump. I think that I have implemented the Angular2 ExceptionHandler as required, but it is never getting called.

Main.ts:

import { bootstrap } from '@angular/platform-browser-dynamic';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
import { AppComponent } from './app.component';
import { ExceptionHandler, Injector , provide, Provider} from '@angular/core';
import { APP_ROUTER_PROVIDERS } from './app.routes';    // <-- load in global routes and bootstrap them
import { ErrorHandler } from './errorhandler/error-handler.component';

bootstrap(AppComponent,[ 
    APP_ROUTER_PROVIDERS,
    disableDeprecatedForms(),
    provideForms(),
    provide(ExceptionHandler, {useClass: ErrorHandler})
])

error-handler.component.ts:

import { Component, Injectable, ExceptionHandler } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { APP_PROVIDERS } from '../app.providers';

@Component({ 
  moduleId: module.id,
  template: `Error happened here!!!!`,
  directives: [ ROUTER_DIRECTIVES ],
  providers: [ APP_PROVIDERS ]
})

@Injectable()
export class ErrorHandler extends ExceptionHandler {

  call (exception: any, stackTrace?: any, reason?: string): void {


  }

}

I have an HTTP error that I'm forcing to occur in one of my services (to test the exception handling) and would expect my "global" handler to get called. Am I missing something? Unfortunately, not a whole lot of documentation out there right now...

TIA.

James

1 Answer 1

3

No idea if that's the problem, but don't use a Component as an error handler. That's not the way to do it.

You just have to (not even necessary, but it looks cooler) implement the ExceptionHandler interface, not extend it. No need for @Injectable() (yet... maybe this handler will have dependencies in the future, and then it is necessary)

export class ErrorHandler implements ExceptionHandler {
  call(error, stackTrace = null, reason = null) {
    // do something with the exception
  }
}

You can keep your bootstrap the way it is

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.