LoginSystem/src/main/java/fr/xephi/authme/util/ExceptionUtils.java
ljacqu 329657bd5f
#1497 Show specific message for invalid YAML files (#1506)
* #1497 Throw dedicated exception for invalid YAML files and handle it on startup
- Wrap SnakeYAML exceptions when loading config.yml and commands.yml on startup into own exception type
- Handle exception type on startup with specific error message

* #1497 Fix inaccurate JavaDoc comment
2018-02-23 23:31:22 +01:00

37 lines
1.2 KiB
Java

package fr.xephi.authme.util;
import com.google.common.collect.Sets;
import java.util.Set;
/**
* Utilities for exceptions.
*/
public final class ExceptionUtils {
private ExceptionUtils() {
}
/**
* Returns the first throwable of the given {@code wantedThrowableType} by visiting the provided
* throwable and its causes recursively.
*
* @param wantedThrowableType the throwable type to find
* @param throwable the throwable to start with
* @param <T> the desired throwable subtype
* @return the first throwable found of the given type, or null if none found
*/
public static <T extends Throwable> T findThrowableInCause(Class<T> wantedThrowableType, Throwable throwable) {
Set<Throwable> visitedObjects = Sets.newIdentityHashSet();
Throwable currentThrowable = throwable;
while (currentThrowable != null && !visitedObjects.contains(currentThrowable)) {
if (wantedThrowableType.isInstance(currentThrowable)) {
return wantedThrowableType.cast(currentThrowable);
}
visitedObjects.add(currentThrowable);
currentThrowable = currentThrowable.getCause();
}
return null;
}
}