Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 704
Kon Avatar asked May 03 '19 18:05

Kon


People also ask

In which type of error scope does an HTTP listener return an error response?

The Mule default error handler logs the error information and returns the event with the error object because it uses an on error propagate scope. This is called rethrowing the error and since this is the calling flow, it returns an error response to the HTTP listener.

How do you handle errors?

Do: Work on exceptions that directly impact the user experience. Don't: Build KPIs around reducing errors or exceptions by a certain percentage or targeting a specific number. Do: Prioritize errors related to your user's personal identifiable information, billing cycle, and functions that could corrupt your database.

What is the use of global error handler in mule 4?

Global Error Handling helps in the reusability of the application's error handling code. Mule has a default error handling framework that takes care of the errors not controlled at the flow level. Mule Global Error Handler helps in defining application-level error handling.

What is global exception handling in Mulesoft?

To begin with, the message exceptions can now be handled at 3 different levels i.e. application, flow, and processor levels, as shown below. The handlers written at the application level are global handlers, which can be used to handle the errors thrown by any flow, which doesn't have its own error handling.


1 Answers

You can try creating a service - ErrorService to share the context and then throw the error from the global error handler. You can then catch the error from the required Component.

PFB the steps:

  1. Create the error service as follows:

    @Injectable({
        providedIn: 'root'
    })
    export class ErrorService {
        constructor() {}
    
        private error = new BehaviorSubject(new Error());
        error_message = this.error.asObservable();
    
        changeMessage(e: Error) {
            this.error.next(e);
        }
    }
    
  2. throw the error from the handleError method in ErrorHandler. PFB the snippet:

    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 {
              //`errorService` is the `instance` for `ErrorService Component` 
              //imported in the defined `ErrorHandler`
              this.errorService.changeMessage(error);
              // Rethrow all other errors.
              throw error;
         }
      }
    
  3. Use try-catch to catch the error in your Component. Use error_message from ErrorService for the same.

like image 190
Jahnavi Paliwal Avatar answered Nov 15 '22 04:11

Jahnavi Paliwal