Support matching against exception causes in ExceptionTypeFilter

Prior to this commit, ExceptionTypeFilter only provided support for
filtering based on exact matches against exception types; however, some
use cases require that filtering be applied to nested causes in a given
exception. For example, this functionality is a prerequisite for
gh-35583.

This commit introduces a new match(Throwable, boolean) method in
ExceptionTypeFilter, where the boolean flag enables matching against
nested exceptions.

See gh-35583
Closes gh-35592
This commit is contained in:
Sam Brannen
2025-10-09 14:19:03 +02:00
parent 6bc3ce4829
commit 670effa02b
3 changed files with 99 additions and 7 deletions
@@ -43,6 +43,11 @@ class ExceptionTypeFilterTests {
assertMatches(new Error());
assertMatches(new Exception());
assertMatches(new RuntimeException());
assertMatchesCause(new Throwable());
assertMatchesCause(new Error());
assertMatchesCause(new Exception());
assertMatchesCause(new RuntimeException());
}
@Test
@@ -67,6 +72,20 @@ class ExceptionTypeFilterTests {
assertDoesNotMatch(new Exception());
}
@Test // gh-35583
void includesCauseAndSubtypeMatching() {
filter = new ExceptionTypeFilter(List.of(IOException.class), null);
assertMatchesCause(new IOException());
assertMatchesCause(new FileNotFoundException());
assertMatchesCause(new RuntimeException(new IOException()));
assertMatchesCause(new RuntimeException(new FileNotFoundException()));
assertMatchesCause(new Exception(new RuntimeException(new IOException())));
assertMatchesCause(new Exception(new RuntimeException(new FileNotFoundException())));
assertDoesNotMatchCause(new Exception());
}
@Test
void excludes() {
filter = new ExceptionTypeFilter(null, List.of(FileNotFoundException.class, IllegalArgumentException.class));
@@ -89,6 +108,20 @@ class ExceptionTypeFilterTests {
assertMatches(new Throwable());
}
@Test // gh-35583
void excludesCauseAndSubtypeMatching() {
filter = new ExceptionTypeFilter(null, List.of(IOException.class));
assertDoesNotMatchCause(new IOException());
assertDoesNotMatchCause(new FileNotFoundException());
assertDoesNotMatchCause(new RuntimeException(new IOException()));
assertDoesNotMatchCause(new RuntimeException(new FileNotFoundException()));
assertDoesNotMatchCause(new Exception(new RuntimeException(new IOException())));
assertDoesNotMatchCause(new Exception(new RuntimeException(new FileNotFoundException())));
assertMatchesCause(new Throwable());
}
@Test
void includesAndExcludes() {
filter = new ExceptionTypeFilter(List.of(IOException.class), List.of(FileNotFoundException.class));
@@ -113,4 +146,16 @@ class ExceptionTypeFilterTests {
.isFalse();
}
private void assertMatchesCause(Throwable candidate) {
assertThat(this.filter.match(candidate, true))
.as("filter '" + this.filter + "' should match " + candidate.getClass().getSimpleName())
.isTrue();
}
private void assertDoesNotMatchCause(Throwable candidate) {
assertThat(this.filter.match(candidate, true))
.as("filter '" + this.filter + "' should not match " + candidate.getClass().getSimpleName())
.isFalse();
}
}