com.github.egatlovs.variablemanager.exceptions.ExceptionHandler Maven / Gradle / Ivy
package com.github.egatlovs.variablemanager.exceptions;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* ExceptionHandler
*
* This class helps creating exceptions inside of the processing classes
* {@code ResultObject} and {@code ProcessingUnit}. Because both of them use
* Reflection inside many situations where exceptions could be thrown are
* present. This class uses the thrown exceptions and maps them to more readable
* messages which help to understand what went wrong.
*
* @author egatlovs
*/
@SuppressWarnings("JavaDoc")
public class ExceptionHandler {
/**
* Maps given Exception to a Result Object Exception.
*
* @param e - thrown Exception
* @param clazz - Class which caused the exception
* @param - The type of the Class causing the exception
* @throws ResultObjectException is thrown at any time
*/
public static void createResultObjectException(Exception e, Class clazz) {
if (e instanceof InstantiationException) {
throw new ResultObjectException(
"Error received while trying to instantiate object of class " + clazz.getName(), e);
} else if (e instanceof IllegalAccessException) {
throw new ResultObjectException(
"Tried to instantiate object of class " + clazz.getName() + " but was not accessible", e);
} else if (e instanceof IllegalArgumentException) {
throw new ResultObjectException("Tried to instantiate object of class " + clazz.getName()
+ " it seems no default constructor is present", e);
} else if (e instanceof InvocationTargetException) {
throw new ResultObjectException(
"Instantiation of object of class " + clazz.getName() + " has thrown an exception ", e);
} else if (e instanceof NoSuchMethodException) {
throw new ResultObjectException("Tried to instantiate object of class " + clazz.getName()
+ " it seems no default constructor is present", e);
} else if (e instanceof SecurityException) {
throw new ResultObjectException(
"Tried to instantiate object of class " + clazz.getName() + " but was not accessible", e);
} else {
throw new ResultObjectException("Unknown Exception occured", e);
}
}
/**
* Maps given Exception to a ResultObjectException.
*
* @param e - thrown Exception
* @param field - Field which caused the Exception
* @param obj - Object which caused the Exception
* @throws ResultObjectException is thrown at any time
*/
public static void createResultObjectException(Exception e, Field field, Object obj) {
if (e instanceof IllegalAccessException) {
throw new ResultObjectException("Tried to access field " + field.getName() + " but was not accessible.", e);
} else if (e instanceof IllegalArgumentException) {
throw new ResultObjectException(
"Tried to access field " + field.getName() + " on wrong object " + obj.toString(), e);
} else {
throw new ResultObjectException("Unknown Exception occured", e);
}
}
/**
* Maps given Exception to a VariableProcessingException.
*
* @param e - thrown Exception
* @param field - Field which caused the Exception
* @param obj - Object which caused the Exception
* @throws VariableProcessingException is thrown at any time
*/
public static void createVariableProcessingException(Exception e, Field field, Object obj) {
if (e instanceof IllegalAccessException) {
throw new VariableProcessingException(
"Tried to access field " + field.getName() + " but was not accessible.", e);
} else if (e instanceof IllegalArgumentException) {
throw new VariableProcessingException(
"Tried to access field " + field.getName() + " on wrong object " + obj.toString(), e);
} else {
throw new VariableProcessingException("Unknown Exception occured", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy