001package co.codewizards.cloudstore.core.util;
002
003import static java.util.Objects.*;
004
005public final class ExceptionUtil {
006
007        private ExceptionUtil() { }
008
009        public static <T extends Throwable> T getCause(final Throwable throwable, final Class<T> searchClass) {
010                requireNonNull(throwable, "throwable");
011                requireNonNull(searchClass, "searchClass");
012
013                Throwable cause = throwable;
014                while (cause != null) {
015                        if (searchClass.isInstance(cause)) {
016                                return searchClass.cast(cause);
017                        }
018                        cause = cause.getCause();
019                }
020                return null;
021        }
022
023        public static RuntimeException throwThrowableAsRuntimeExceptionIfNeeded(final Throwable throwable) {
024                requireNonNull(throwable, "throwable");
025                if (throwable instanceof Error)
026                        throw (Error) throwable;
027
028                if (throwable instanceof RuntimeException)
029                        throw (RuntimeException) throwable;
030
031                throw new RuntimeException(throwable);
032        }
033}