Search in sources :

Example 6 with ErrorInformation

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;
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) ArrayList(java.util.ArrayList)

Example 7 with 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;
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ActivitiException(org.activiti.engine.ActivitiException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) SQLException(java.sql.SQLException) JpaSystemException(org.springframework.orm.jpa.JpaSystemException) ActivitiClassLoadingException(org.activiti.engine.ActivitiClassLoadingException) ELException(org.activiti.engine.impl.javax.el.ELException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) OptimisticLockException(javax.persistence.OptimisticLockException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodNotAllowedException(org.finra.herd.model.MethodNotAllowedException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(javax.persistence.PersistenceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ErrorInformation (org.finra.herd.model.api.xml.ErrorInformation)7 SQLException (java.sql.SQLException)3 PersistenceException (javax.persistence.PersistenceException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 JAXBException (javax.xml.bind.JAXBException)2 ActivitiClassLoadingException (org.activiti.engine.ActivitiClassLoadingException)2 ActivitiException (org.activiti.engine.ActivitiException)2 ELException (org.activiti.engine.impl.javax.el.ELException)2 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)2 MethodNotAllowedException (org.finra.herd.model.MethodNotAllowedException)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1