1

I´m trying to implement a central ExceptionHandler in Angular 2. I searched in SO and found several topics about this subject, like those bellow:

Module '"angular2/angular2"' has no exported member 'ExceptionHandler'

How to properly overwrite the exceptionHandler in angularjs?

But, this topics seem to be outdated.

I´m using Angular 2 RC5, and following the documentation here, I tried to implement:

import { ExceptionHandler } from '@angular/core';
export class CustomExceptionHandler implements ExceptionHandler {

call(error:any, stackTrace:any = null, reason:string = null) {
    // do something with the exception
}

}

But I received a error like that "TS2420 - CustomExceptionHandler incorrectly implements interface ExceptionHandler. Property '_logger' is missing in type 'CustomExceptionHandler'.

I´am a newbie in TS, but I have been programming in Java for 10 years.

Actually, when I click on ExceptionHandler in IDE (IntelliJ), the code follow up to a class, not a interface.

export declare class ExceptionHandler {
    private _logger;
    private _rethrowException;
    constructor(_logger: any, _rethrowException?: boolean);
    static exceptionToString(exception: any, stackTrace?: any, reason?: string): string;
    call(exception: any, stackTrace?: any, reason?: string): void;
}
Community
  • 1
  • 1
etc
  • 83
  • 2
  • 9
  • Have a look at this: http://www.bennadel.com/blog/3138-creating-a-custom-errorhandler-in-angular-2-rc-6.htm. It seems there have been breaking changes to error handling with RC6, so you might wanna think about updating. – j2L4e Sep 06 '16 at 19:00
  • @etc: where you able to resolve this? did you try the below solution? – Madhu Ranjan Sep 08 '16 at 15:11
  • Thank you! Works like a charm. – etc Sep 09 '16 at 17:50

1 Answers1

3

Below is for RC6,

Exceptions is deprecated for public use see changelog for RC6,

core: Exceptions are no longer part of the public API. We don't expect that anyone should be referring to the Exception types.

ExceptionHandler.call(exception: any, stackTrace?: any, reason?: string): void;

change to:

ErrorHandler.handleError(error: any): void;

For implementation using ErrorHandler, see this

class MyErrorHandler implements ErrorHandler {
  call(error, stackTrace = null, reason = null) {
    // do something with the exception
  }
}
@NgModule({
  providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
})
class MyModule {}

Hope this helps!!

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69
  • Thank you guys. Works for me. – etc Sep 09 '16 at 17:51
  • Thank you for the answer, but looks like this information is obsolete, actual documentation could be found on the official site https://angular.io/api/core/ErrorHandler – Eugene Dec 29 '18 at 12:54