Package com.github.toolarium.common.util
Class ExceptionWrapper
java.lang.Object
com.github.toolarium.common.util.ExceptionWrapper
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:
to:
To support logging in the exception case add the log level. There are additional parameters to manipulate the log behavior.
To change the behavior how an exception will be converted just implement your own ExceptionWrapper.ExceptionHandler and set it with
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(...)-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassThis class implements the default exception handler behaviorstatic interfaceDefines the exception handler API -
Method Summary
Modifier and TypeMethodDescription<T extends Throwable>
TconvertException(Throwable t, Class<T> newExceptionClass) This method converts a given exception into an another exception<T extends Throwable>
TconvertException(Throwable t, Class<T> newExceptionClass, org.slf4j.event.Level logLevel) This method converts a given exception into an another exception<T extends Throwable>
TconvertException(Throwable t, Class<T> newExceptionClass, org.slf4j.event.Level logLevel, String logMessage) This method converts a given exception into an another exceptionstatic ExceptionWrapperGet the instancevoidsetExceptionHandler(ExceptionWrapper.ExceptionHandler exceptionHandler) Sets the exception handler
-
Method Details
-
getInstance
Get the instance- Returns:
- the instance
-
setExceptionHandler
Sets the exception handler- Parameters:
exceptionHandler- the exception handler- Throws:
IllegalArgumentException- In case of an invalid exception handler
-
convertException
This method converts a given exception into an another exception- Type Parameters:
T- the generic type- Parameters:
t- the exception to wrapnewExceptionClass- 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 wrapnewExceptionClass- the new exception classlogLevel- 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 wrapnewExceptionClass- the new exception classlogLevel- the log levellogMessage- the log message- Returns:
- the wrapped exception
-