1

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 { }
Daniel Patrick
  • 3,980
  • 6
  • 29
  • 49
  • `import { Service } from '../services/a.service';` should probably be `import { ServiceA } from '../services/a.service';` (missing `A` in the service name) – Günter Zöchbauer Sep 19 '16 at 15:25

2 Answers2

1

You need to add CustomExceptionHandler and its dependencies to providers as well:

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [
    ServiceA,
    CustomExceptionHandler,
    { provide: ExceptionHandler, useClass: CustomExceptionHandler },
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }
Daniel Patrick
  • 3,980
  • 6
  • 29
  • 49
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I've revised the error: `Uncaught Can't resolve all parameters for CustomExceptionHandler: (?).` – Daniel Patrick Sep 19 '16 at 15:24
  • this isn't it, the issue persists. thank you for your time though – Daniel Patrick Sep 19 '16 at 15:26
  • thank you both Gunter and ade for your answers. it was the combination of both of these that fixed it. You need to add @Injectable() and you need to list it as a normal provider in the app.module.ts in addition to listing it as a useClass to override the ExceptionHandler. This leaves me with somewhat of a predicament for who to accept as the answer. Whomever types up the combination of these will get it. Thanks again! – Daniel Patrick Sep 19 '16 at 15:32
  • 1
    Everyone has to start somewhere. I didn't start with 100k neither ;-) – Günter Zöchbauer Sep 19 '16 at 15:39
1

update ExceptionHandler was renamed to ErrorHandler https://stackoverflow.com/a/35239028/217408

orgiginal

Add @Injectable() to your CustomExceptionHandler class.

Add CustomExceptionHandler as another one of your providers in the app.module.ts:

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [
    ServiceA,
    CustomExceptionHandler,
    { provide: ExceptionHandler, useClass: CustomExceptionHandler },
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }
Community
  • 1
  • 1
ade jones
  • 639
  • 11
  • 15
  • thank you both Gunter and ade for your answers. it was the combination of both of these that fixed it. You need to add @Injectable() and you need to list it as a normal provider in the app.module.ts in addition to listing it as a useClass to override the ExceptionHandler. This leaves me with somewhat of a predicament for who to accept as the answer. Whomever types up the combination of these will get it. Thanks again! – Daniel Patrick Sep 19 '16 at 15:32