Angular (4): Custom Error Handler & DI: Cannot instantiate cyclic dependency
We have to manually call the injector with the service name in the execution of the handleError function.
So I had to use the built in Inject
to inject my service dependency. The end result of the custom-error-handler
class which calls the PopupService
which has the MdSnackBar
dependency injected looks like this:
@Injectable()
export class CustomErrorHandler implements ErrorHandler {
constructor(private injector: Injector) {
}
handleError(error: any): void {
console.log(error);
const popupService = this.injector.get(PopupService);
}
}
And this is the PopupService:
@Injectable()
export class PopupService {
constructor(private snackBar: MdSnackBar) {
}
public throwErrorPopup(textOfError: string) {
this.snackBar.open(textOfError, 'X', {
duration: environment.snackBarTime,
});
}
}
Solution mentioned in here: https://medium.com/@amcdnl/global-error-handling-with-angular2-6b992bdfb59c