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;
}
3
  • Have a look at this: bennadel.com/blog/…. It seems there have been breaking changes to error handling with RC6, so you might wanna think about updating. Commented Sep 6, 2016 at 19:00
  • @etc: where you able to resolve this? did you try the below solution? Commented Sep 8, 2016 at 15:11
  • Thank you! Works like a charm. Commented Sep 9, 2016 at 17:50

1 Answer 1

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!!

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

2 Comments

Thank you guys. Works for me.
Thank you for the answer, but looks like this information is obsolete, actual documentation could be found on the official site angular.io/api/core/ErrorHandler

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.