Class ExceptionWrapper

java.lang.Object
com.github.toolarium.common.util.ExceptionWrapper

public final class ExceptionWrapper extends Object
This class implements the mechanism to wrap exceptions. The aim of this helper class is to minimize exception handling code blocks. The following code can be simplified by this class from:
 try {
     ...
 } catch(XYException e) {
      IllegalargumentException ex = new IllegalargumentException(e. getMessage());
      ex.setStackTrace(e.getStackTrace());
      
      // optional logging
      log.error("Exception " + e.getClass().getName() + " occured: " + e.getMessage(), e);
      
      throw ex;
 }
 


to:
 try {
     ...
 } catch(XYException e) {
      throw (IllegalArgumentException)
          ExceptionWrapper.convertException(e, IllegalArgumentException.class.getName());
 }
 


To support logging in the exception case add the log level. There are additional parameters to manipulate the log behavior.
 try {
     ...
 } catch(XYException e) {
      throw (IllegalArgumentException)
          ExceptionWrapper.convertException(e, IllegalArgumentException.class.getName(), org.slf4j.event.Level.WARN);
 }
 


To change the behavior how an exception will be converted just implement your own ExceptionWrapper.ExceptionHandler and set it with ExceptionWrapper.getInstance().setExceptionHandler(...)
  • Method Details

    • getInstance

      public static ExceptionWrapper getInstance()
      Get the instance
      Returns:
      the instance
    • setExceptionHandler

      public void setExceptionHandler(ExceptionWrapper.ExceptionHandler exceptionHandler)
      Sets the exception handler
      Parameters:
      exceptionHandler - the exception handler
      Throws:
      IllegalArgumentException - In case of an invalid exception handler
    • convertException

      public <T extends Throwable> T convertException(Throwable t, Class<T> newExceptionClass)
      This method converts a given exception into an another exception
      Type Parameters:
      T - the generic type
      Parameters:
      t - the exception to wrap
      newExceptionClass - the new exception class
      Returns:
      the wrapped exception
    • convertException

      public <T extends Throwable> T convertException(Throwable t, Class<T> newExceptionClass, org.slf4j.event.Level logLevel)
      This method converts a given exception into an another exception
      Type Parameters:
      T - the generic type
      Parameters:
      t - the exception to wrap
      newExceptionClass - the new exception class
      logLevel - the log level
      Returns:
      the wrapped exception
    • convertException

      public <T extends Throwable> T convertException(Throwable t, Class<T> newExceptionClass, org.slf4j.event.Level logLevel, String logMessage)
      This method converts a given exception into an another exception
      Type Parameters:
      T - the generic type
      Parameters:
      t - the exception to wrap
      newExceptionClass - the new exception class
      logLevel - the log level
      logMessage - the log message
      Returns:
      the wrapped exception