Support direct matching against exceptions in ExceptionTypeFilter

Prior to this commit, ExceptionTypeFilter only supported matching
against an exception type. However, most use cases involve matching
against an exception instance. Moreover, every use case within the core
Spring Framework uses ExceptionTypeFilter to match against concrete
exception instances.

This commit therefore introduces an overloaded match(Throwable) method
in ExceptionTypeFilter in order to provide support for the most common
use cases.

See gh-35109
Closes gh-35160
This commit is contained in:
Sam Brannen
2025-07-05 13:13:47 +02:00
parent 33f51b183d
commit 17df4b4c38
8 changed files with 57 additions and 45 deletions
@@ -62,7 +62,7 @@ class CachePutInterceptor extends AbstractKeyCacheInterceptor<CachePutOperation,
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyPut && operation.getExceptionTypeFilter().match(original.getClass())) {
if (!earlyPut && operation.getExceptionTypeFilter().match(original)) {
cacheValue(context, value);
}
throw ex;
@@ -58,7 +58,7 @@ class CacheRemoveAllInterceptor extends AbstractCacheInterceptor<CacheRemoveAllO
}
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyRemove && operation.getExceptionTypeFilter().match(original.getClass())) {
if (!earlyRemove && operation.getExceptionTypeFilter().match(original)) {
removeAll(context);
}
throw ex;
@@ -56,12 +56,12 @@ class CacheRemoveEntryInterceptor extends AbstractKeyCacheInterceptor<CacheRemov
}
return result;
}
catch (CacheOperationInvoker.ThrowableWrapper wrapperException) {
Throwable ex = wrapperException.getOriginal();
if (!earlyRemove && operation.getExceptionTypeFilter().match(ex.getClass())) {
catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
if (!earlyRemove && operation.getExceptionTypeFilter().match(original)) {
removeValue(context);
}
throw wrapperException;
throw ex;
}
}
@@ -92,7 +92,7 @@ class CacheResultInterceptor extends AbstractKeyCacheInterceptor<CacheResultOper
if (exceptionCache == null) {
return;
}
if (filter.match(ex.getClass())) {
if (filter.match(ex)) {
doPut(exceptionCache, cacheKey, ex);
}
}