Wednesday, 8 May 2019

How to limit scope of ErrorHandler?

I have a global error handler defined as such (simplified/proprietary info scrubbed):

export class ErrorsHandler extends CommonBase implements ErrorHandler {
  constructor(protected loggingService: LoggingService,
              private coreService: CoreService {

    super(loggingService);
  }

  handleError(error: Error) {
    if (error && error.stack && (error.stack.indexOf(Constants.PACKAGE_NAME) >= 0)) {
      this.logSystemError(error, true);
      this.coreService.showSystemError(error, this.owner);
    }
    else {
      // Rethrow all other errors.
      throw error;
    }
  }

And in my module (and only my module), it's registered as a provider as such:

export function errorHandlerFactory(loggingService: LoggingService, coreService: CoreService) {
  return new ErrorsHandler(loggingService, coreService);
}

providers: [
    { provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [LoggingService, CoreService] }
]

My module is consumed by others, and together we make up one large application. My problem is that ALL script errors are caught, even though I try to filter for those that are only relevant to my module/package, because the filtering is done within handleError(). And even though I rethrow errors that are not relevant to me (in the else above), developers of other modules/packages are complaining that I'm globally catching everything and the rethrown errors they get already lose certain context/information.

So the question is, is it possible to somehow limit the scope of my error handler to catch and handle ONLY script errors originating from my module/package (while completely ignore all other script errors within the application)?

After much googling, the only alternative I can think of is putting try/catch everywhere, which is something I'd like to avoid if at all possible.



from How to limit scope of ErrorHandler?

No comments:

Post a Comment