1

I have two classes in angular and typescript. One class CustomErrorHandlerService is inherting from angular error handler which provides an callback and the other class ErrorProessor is processing that error by using the callback. The problem is that the callback is never getting executed and instead I am getting the following error. I need 2 different classes as I am avoiding some circular dependency errors.

Error: this.handleErrorCallback is not a function

import { Injectable, ErrorHandler } from '@angular/core';

@Injectable()
export class CustomErrorHandlerService implements ErrorHandler{

  constructor() { 
  }

  public handleErrorCallback: (error: Error) => void;

  handleError(error: Error) {

    console.log('Via Custom Error Handler: ' + error.toString());
    this.handleErrorCallback(error);
  }

}



import { Injectable } from '@angular/core';
import { CustomErrorHandlerService } from './app/custom-error-handler.service';

@Injectable()
export class ErrorProcessor {
  constructor(private customErrorHandler: CustomErrorHandlerService) {
    this.customErrorHandler.handleErrorCallback = this.handleError;
  }

  public handleError(): void {
    console.log('error processed');
  }
}

Thanks.

Naren
  • 139
  • 1
  • 17
  • `this.customErrorHandler.handleErrorCallback = this.handleError.bind(this);` is one way of solving it. – eko Jun 21 '21 at 20:38
  • how are you registering `ErrorProcessor`. Its not registered in root based on your example. – O.MeeKoh Jun 21 '21 at 20:44
  • I did the change and I still see the same error and I am not sure why. – Naren Jun 21 '21 at 20:44
  • @O.Meekoh I added @Injectable({ providedIn: 'root', }) But still that does not make any difference if that is what you are asking. – Naren Jun 21 '21 at 20:49
  • @eko any suggestions? – Naren Jun 22 '21 at 15:08
  • I think I was wrong initially but there are several errors here. 1) You wrote "inheriting" but you're implementing the other class. 2) On first look the `handleErrorCallback` is already `undefined` and you're trying to set this to something which I guess is not working. So how are you injecting these providers? – eko Jun 22 '21 at 19:47

0 Answers0