use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class HerdErrorInformationExceptionHandler method getErrorInformation.
/**
* Gets a new error information based on the specified message.
*
* @param httpStatus the status of the error.
* @param exception the exception whose message will be used.
*
* @return the error information.
*/
private ErrorInformation getErrorInformation(HttpStatus httpStatus, Throwable exception) {
ErrorInformation errorInformation = new ErrorInformation();
errorInformation.setStatusCode(httpStatus.value());
errorInformation.setStatusDescription(httpStatus.getReasonPhrase());
String errorMessage = exception.getMessage();
if (StringUtils.isEmpty(errorMessage)) {
errorMessage = exception.getClass().getName();
}
errorInformation.setMessage(errorMessage);
List<String> messageDetails = new ArrayList<>();
Throwable causeException = exception.getCause();
while (causeException != null) {
messageDetails.add(causeException.getMessage());
causeException = causeException.getCause();
}
errorInformation.setMessageDetails(messageDetails);
return errorInformation;
}
use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class HerdErrorInformationExceptionHandler method isReportableError.
/**
* Returns whether the specified exception is one that should be reported (i.e. a support team should be notified in some way).
*
* @param exception the exception to analyze.
*
* @return true if the exception is reportable or false if not.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean isReportableError(Throwable exception) {
// By default, the exception is reportable (i.e. the safe route).
boolean isReportable = true;
// Only proceed if we have an exception (as opposed to another Throwable) since the exception resolver only works off "exceptions".
if (exception instanceof Exception) {
// Try to resolve the exception which should yield a method on our exception handler (i.e. this class).
Method method = resolver.resolveMethod((Exception) exception);
// whether or not the exception is reportable.
if ((method != null) && (ErrorInformation.class.isAssignableFrom(method.getReturnType()))) {
// Create a list of parameters we will need to pass to the method being invoking.
List<Object> parameterValues = new ArrayList<>();
// Get the method parameters as an array of "classes".
Class[] parameterTypes = method.getParameterTypes();
// and not throw a null pointer exception.
for (Class clazz : parameterTypes) {
if (clazz.isAssignableFrom(exception.getClass())) {
// The parameter class is assignable from the actual exception so pass the exception itself.
parameterValues.add(exception);
} else {
// The parameter class is something else so pass null.
parameterValues.add(null);
}
}
try {
// Invoke the handler method specific to our exception and get the error information back.
ErrorInformation errorInformation = (ErrorInformation) method.invoke(this, parameterValues.toArray());
// The only error information status that is reportable is "internal server error" so set the flag to false for all other cases.
if (errorInformation.getStatusCode() != HttpStatus.INTERNAL_SERVER_ERROR.value()) {
isReportable = false;
}
} catch (IllegalAccessException | InvocationTargetException ex) {
logError("Unable to invoke method \"" + method.getDeclaringClass().getName() + "." + method.getName() + "\" so couldn't determine if exception is reportable. Defaulting to true.", ex);
}
}
}
// Return whether the error should be reported.
return isReportable;
}
Aggregations