I have the following code :
app.module.ts:
NgModule({
declarations: [
AppComponent
],
imports: [
RouterModule,
BrowserModule,
ReactiveFormsModule,
FormsModule,
HttpModule,
AppRoutingModule // Routes
],
providers: [ // services
AppLog,
{provide: ErrorHandler, useClass: MyErrorHandler}
],
bootstrap: [AppComponent]
})
export class AppModule {}
MyErrorHandler.ts:
@Injectable()
export class MyErrorHandler implements ErrorHandler {
constructor (private _appLog: AppLog) {}
handleError(error:any):void {
let errorMessage: string = "" + error
this._appLog.logMessageAsJson(errorMessage, "error")
.subscribe(
...
)
}
}
appLog.ts
@Injectable()
export class AppLog {
constructor (private _http: Http) {}
logMessageAsJson(message: string, type: string) {
let headers = new Headers({ "Content-Type": "application/json" })
let jsonMessage = {"type": type, "message": message}
return this._http.post(JSON.stringify(jsonMessage), headers)
}
}
However, when my app bootstrap, it fails if I have an injection in MyErrorHandler
with the following error :
Unhandled Promise rejection: Provider parse errors:
Cannot instantiate cyclic dependency!
If I do remove constructor (private _appLog: AppLog) {}
and then do something else in handleError
it works just fine and the ErrorHandler is called.
I guess it doesn't work as AppLog and MyErrorHandler are instantiated at the same time