Skip to content

Commit

Permalink
HSEARCH-3170 Use meaningful messages when nesting exceptions, relying…
Browse files Browse the repository at this point in the history
… on the cause chain if necessary

InvocationTargetException, in particular, will not show any message,
which used to result in something like "Got exception: null" in the
failure report. Now we will try to get the message for the cause of that
InvocationTargetException, which may be more informative.
  • Loading branch information
yrodiere committed Dec 11, 2018
1 parent 677ef5c commit a446b40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.search.util.SearchException;
import org.hibernate.search.util.impl.common.LoggerFactory;
import org.hibernate.search.util.impl.common.StringHelper;
import org.hibernate.search.util.impl.common.Throwables;

/**
* Utility class to load instances of other classes by using a fully qualified name,
Expand Down Expand Up @@ -100,10 +101,12 @@ public static <T> T untypedInstanceFromClass(final Class<T> classToLoad, final S
}
catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
if ( StringHelper.isEmpty( componentDescription ) ) {
throw log.unableToInstantiateClass( classToLoad, e.getMessage(), e );
throw log.unableToInstantiateClass( classToLoad, Throwables.getFirstNonNullMessage( e ), e );
}
else {
throw log.unableToInstantiateComponent( componentDescription, classToLoad, e.getMessage(), e );
throw log.unableToInstantiateComponent(
componentDescription, classToLoad, Throwables.getFirstNonNullMessage( e ), e
);
}
}
}
Expand Down Expand Up @@ -161,7 +164,9 @@ public static <T> T instanceFromClass(Class<T> targetSuperType, Class<?> classTo
instance = singleMapConstructor.newInstance( constructorParameter );
}
catch (Exception e) {
throw log.unableToInstantiateComponent( componentDescription, classToLoad, e.getMessage(), e );
throw log.unableToInstantiateComponent(
componentDescription, classToLoad, Throwables.getFirstNonNullMessage( e ), e
);
}
return verifySuperTypeCompatibility( targetSuperType, instance, classToLoad, componentDescription );
}
Expand Down
Expand Up @@ -63,4 +63,13 @@ public static <T extends Throwable> T combine(T throwable, T otherThrowable) {
return toThrow;
}

public static String getFirstNonNullMessage(Throwable t) {
Throwable cause = t.getCause();
while ( t.getMessage() == null && cause != null ) {
t = cause;
cause = t.getCause();
}
return t.getMessage();
}

}

0 comments on commit a446b40

Please sign in to comment.