etc
etc

Reputation: 83

Custom ExceptionHandler in Angular 2

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;
}

Upvotes: 1

Views: 2494

Answers (1)

Madhu Ranjan
Madhu Ranjan

Reputation: 17944

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

Upvotes: 3

Related Questions