When trying to inject a service into my CustomExceptionHandler, the Angular injector cannot find the service.
The error: Uncaught Can't resolve all parameters for CustomExceptionHandler: (?).
The setup:
CustomExceptionHandler
import { ExceptionHandler } from '@angular/core';
import { ServiceA } from '../services/a.service';
export class CustomExceptionHandler extends ExceptionHandler {
constructor(private serviceA: ServiceA) {
super(null, null);
}
call(error, stackTrace = null, reason = null) {
//do something with the service
}
}
app.module
@NgModule({
declarations: [
...
],
imports: [
...
],
providers: [
ServiceA,
{ provide: ExceptionHandler, useClass: CustomExceptionHandler },
],
bootstrap: [AppComponent],
})
export class AppModule { }
You need to add
CustomExceptionHandler
and its dependencies toproviders
as well:update
ExceptionHandler
was renamed toErrorHandler
https://stackoverflow.com/a/35239028/217408orgiginal
Add
@Injectable()
to your CustomExceptionHandler class.Add CustomExceptionHandler as another one of your providers in the app.module.ts: