package to.etc.util;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import to.etc.function.RunnableEx;
import to.etc.function.SupplierEx;

/**
 * ExceptionUtil
 * A collection of utility methods for working with Exceptions
 */
@NonNullByDefault
final public class ExceptionUtil {
	private ExceptionUtil() {}

	/**
	 * If runnable throws an exception, wraps the checked exception into a runtime.
	 * @param runnableEx runnable
	 */
	public static void silentThrows(RunnableEx runnableEx)  {
		try {
			runnableEx.run();
		}catch(Exception ex) {
			throw WrappedException.wrap(ex);
		}
	}

	/**
	 * If supplier throws an exception, wraps the checked exception into a runtime.
	 * @param supplierEx runnable
	 */
	public static <T> T silentThrows(SupplierEx<T> supplierEx) {
		try {
			return supplierEx.get();
		} catch(Exception e) {
			throw WrappedException.wrap(e);
		}
	}

	/**
	 * This method will run the runnable and if runnable throws an exception, the exception is ignored.
	 * Use sparingly!
	 * @param runnableEx runnable
	 */
	public static void silentFails(RunnableEx runnableEx) {
		try {
			runnableEx.run();
		}catch(Exception ignored){}
	}

	/**
	 * Runs the supplier. If the supplier fails, returns the default value.
	 * @param supplierEx supplier
	 * @param defaultValue default value
	 * @param <T> any type.
	 * @return value from the supplier or defaultValue
	 */
	@Nullable
	public static <T> T silentFails(SupplierEx<T> supplierEx, @Nullable T defaultValue) {
		try {
			return supplierEx.get();
		}catch(Exception ignored) {
			return defaultValue;
		}
	}
}

@@ -182,16 +182,83 @@
 ierEx;%0A%0A
+import java.sql.SQLException;%0Aimport java.util.function.Function;%0A%0A
 /**%0A * E
@@ -1691,10 +1691,644 @@
 %0A%09%09%7D%0A%09%7D%0A
+%0A%09/**%0A%09 * Walk an entire exception tree to find something specific, which is then returned.%0A%09 */%0A%09@Nullable%0A%09public static %3CT%3E T findException(Throwable in, Function%3CThrowable, T%3E matcher) %7B%0A%09%09T res = matcher.apply(in);%0A%09%09if(null != res)%0A%09%09%09return res;%0A%0A%09%09if(in instanceof SQLException) %7B%0A%09%09%09SQLException sx = (SQLException) in;%0A%09%09%09SQLException next = sx.getNextException();%0A%09%09%09if(next != in && next != null) %7B%0A%09%09%09%09res = findException(next, matcher);%0A%09%09%09%09if(null != res)%0A%09%09%09%09%09return res;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09Throwable cause = in.getCause();%0A%09%09if(cause != null && cause != in) %7B%0A%09%09%09return findException(cause, matcher);%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%0A
 %7D%0A

add method to find something in an exception tree