I'm trying to inject an Angular Material 2 element into a custom error handler I wrote, but I keep getting this error:
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
This is the error handler class:
@Injectable()
export class CustomErrorHandler implements ErrorHandler {
constructor(private snackBar: MdSnackBar) {
}
handleError(error: any): void {
//FIXME: display popup
console.error('Error occurred!');
console.error(error);
this.snackBar.open('Application Error: ' + error, 'X', {
duration: environment.snackBarTime,
});
}
}
This is the providers part of app.module
providers: [
SearchService,
CompanyService,
MdSnackBar,
PopupService,
AuthService,
LoginService,
AuthGuard,
ErrorService,
TokenStorageService,
BankService,
CountryService,
{provide: ErrorHandler, useClass: CustomErrorHandler},
{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true},
],
exports: [
TranslateModule
],
bootstrap: [AppComponent]
})
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With