Reputation: 91
I developed a custom error handler which implements Angular2 Error Handler class. My custom error handler uses a logger service to log errors. The code looks like as follows:
export class CustomErrorHandler implements ErrorHandler {
constructor(private logger: LoggerService) {}
handleError(error: any): void {
logger.error('....');
}
}
However, since the logger service uses Angular2 router, I cannot inject the logger service to the custom error handler! Running the above code throws the following exception!
Error: Provider parse errors:↵Cannot instantiate cyclic dependency!
Upvotes: 0
Views: 449
Reputation: 5656
You need to inject manually, to avoid the cyclic dependency problem because this class is created before the providers, your code should be:
import { Injectable, Injector } from '@angular/core';
import { Logger } from '...';
@Injectable()
export class CustomErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {}
handleError(error: any): void {
const logger = this.injector.get(Logger);
logger.error('....');
}
}
Upvotes: 1