Class ExceptionHandler

java.lang.Object
org.mule.sdk.api.runtime.exception.ExceptionHandler

@MinMuleVersion("4.5.0") public abstract class ExceptionHandler extends Object
Allows the developer to provide generic logic to enrich exceptions, either via logging, sending notifications, etc.

The developer can return a new exception which replaces the original one or return the one that was thrown by the operation. For example, wrapping the Exception into a ConnectionException, the runtime know that reconnection is needed. Notice that this implies that the method should not fail by any reason.

Since:
1.0
  • Constructor Details

    • ExceptionHandler

      public ExceptionHandler()
  • Method Details

    • enrichException

      public abstract Exception enrichException(Exception e)
      This method can return a new enriched exception or the original exception after doing some processing with it. It must not return a null value. Also the implementation of this method needs to be thread safe and must not fail.
      Parameters:
      e - the exception thrown by the operation
      Returns:
      an Enriched Exception
      Since:
      1.0
    • getRootErrorException

      protected Exception getRootErrorException(Exception exception)
      This method instrospects the given exception looking for the deepest ErrorTypeDefinition in the causes. Returns a a new ModuleException that wraps the exception received as parameter with the ErrorTypeDefinition found.
      Parameters:
      exception - to introspect
      Returns:
      ModuleException with the innermost ErrorTypeDefinition found.
    • getCauseOfType

      protected <T extends Throwable> Optional<T> getCauseOfType(Throwable throwable, Class<T> causeType)
      Returns the first cause of throwable which is an instance of causeType. If throwable is an instance of causeType itself, then throwable is returned.
      Type Parameters:
      T - the generic type of the expected exception
      Parameters:
      throwable - the exception to introspect
      causeType - the cause type
      Returns:
      an optional cause