Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Sep 30, 2021
2 parents 60c23a7 + dd0dc25 commit 74e0c0d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>jre-utils</artifactId>
<version>0.2.4</version>
<version>0.2.5</version>
<name>JreUtils</name>
<packaging>jar</packaging>

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/info/unterrainer/commons/jreutils/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void swallow(final Runnable runnable, final Class<?>... exceptions
public static <T> T swallowReturning(final Supplier<T> supplier, final Class<?>... exceptions) {
try {
return supplier.get();
} catch (Exception e) {
} catch (Throwable e) {
if (!containsException(e, exceptions))
throw e;
return null;
Expand Down Expand Up @@ -95,7 +95,7 @@ public static <T> T retryReturning(final int times, final long backOffInMillis,
for (int i = 0; i < times; i++)
try {
result = supplier.get();
} catch (Exception e) {
} catch (Throwable e) {
if (!containsException(e, exceptions) || i == times - 1)
throw e;
try {
Expand All @@ -107,10 +107,19 @@ public static <T> T retryReturning(final int times, final long backOffInMillis,
return result;
}

public static boolean containsException(final Exception e, final Class<?>... exceptions) {
public static boolean containsException(final Throwable e, final Class<?>... exceptions) {
for (Class<?> omit : exceptions)
if (omit.isAssignableFrom(e.getClass()))
if (equalsOrCauseEquals(e, omit))
return true;
return false;
}

private static boolean equalsOrCauseEquals(final Throwable e, final Class<?> omit) {
if (omit.isAssignableFrom(e.getClass()))
return true;
Throwable cause = e.getCause();
if (cause != null && equalsOrCauseEquals(cause, omit))
return true;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package info.unterrainer.commons.jreutils;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class ExceptionsTests {

@Test
public void BeforeTest() {
public void codeBeforeTest() {
try {
// Some method you don't have control over throwing an exception.
throw new IllegalArgumentException();
Expand All @@ -15,9 +18,34 @@ public void BeforeTest() {
}

@Test
public void AfterTest() {
public void throwsExceptionWithoutSwallow() {
assertThrows(IllegalArgumentException.class, () -> {
// Some method you don't have control over throwing an exception.
throw new IllegalArgumentException();
});
}

@Test
public void swallowsWhenExceptionMatches() {
Exceptions.swallow(() -> {
throw new IllegalArgumentException();
}, IllegalArgumentException.class, NumberFormatException.class);
assertTrue(true);
}

@Test
public void swallowsWhenCauseMatches() {
Exceptions.swallow(() -> {
throw new IllegalArgumentException("", new NumberFormatException());
}, NumberFormatException.class);
assertTrue(true);
}

@Test
public void swallowsWhenMultiLayeredCauseMatches() {
Exceptions.swallow(() -> {
throw new IllegalArgumentException("", new IllegalArgumentException("", new NumberFormatException()));
}, NumberFormatException.class);
assertTrue(true);
}
}

0 comments on commit 74e0c0d

Please sign in to comment.