Skip to content

Commit

Permalink
🔇 silent changes: add reflection4j and traverse4j #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Oct 7, 2024
1 parent e2deaa8 commit 18fe1e2
Show file tree
Hide file tree
Showing 4 changed files with 948 additions and 0 deletions.
32 changes: 32 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/Exception4j.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.unify4j.common;

public final class Exception4j {

/**
* Safely ignores a given Throwable or rethrows it if it is an error that should not be ignored.
* Specifically, it checks for {@link OutOfMemoryError} and rethrows it, as it should not be suppressed.
* Other types of {@link Throwable} are ignored.
*
* @param t The {@link Throwable} to possibly ignore. If it is an instance of {@link OutOfMemoryError}, it is rethrown.
*/
public static void safelyIgnoreException(Throwable t) {
if (t instanceof OutOfMemoryError) {
throw (OutOfMemoryError) t;
}
}

/**
* Retrieves the deepest (most nested) cause of a given {@link Throwable}.
* This method iteratively traverses the exception chain via {@link Throwable#getCause()}
* to find and return the root cause of the exception.
*
* @param e The {@link Throwable} whose deepest cause is to be found.
* @return The deepest (root) cause {@link Throwable}.
*/
public static Throwable getDeepestException(Throwable e) {
while (e.getCause() != null) {
e = e.getCause();
}
return e;
}
}
Loading

0 comments on commit 18fe1e2

Please sign in to comment.