Skip to content

Commit e32a2f3

Browse files
committed
Propagate method error in some cases of reactive findInCaches errors
In a Cacheable reactive method, if an exception is propagated from both the method and the caching infrastructure, an NPE could previously surface due to the `CacheAspectSupport` attempting to perform an `onErrorResume` with a `null`. This change ensures that in such a case the user-level exception from the method is propagated instead. Closes gh-33492
1 parent 9f4968e commit e32a2f3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,8 @@ public Object findInCaches(CacheOperationContext context, Cache cache, Object ke
11411141
.onErrorResume(RuntimeException.class, ex -> {
11421142
try {
11431143
getErrorHandler().handleCacheGetError((RuntimeException) ex, cache, key);
1144-
return evaluate(null, invoker, method, contexts);
1144+
Object e = evaluate(null, invoker, method, contexts);
1145+
return (e != null ? e : Flux.error((RuntimeException) ex));
11451146
}
11461147
catch (RuntimeException exception) {
11471148
return Flux.error(exception);
@@ -1155,7 +1156,8 @@ public Object findInCaches(CacheOperationContext context, Cache cache, Object ke
11551156
.onErrorResume(RuntimeException.class, ex -> {
11561157
try {
11571158
getErrorHandler().handleCacheGetError((RuntimeException) ex, cache, key);
1158-
return evaluate(null, invoker, method, contexts);
1159+
Object e = evaluate(null, invoker, method, contexts);
1160+
return (e != null ? e : Mono.error((RuntimeException) ex));
11591161
}
11601162
catch (RuntimeException exception) {
11611163
return Mono.error(exception);

spring-context/src/test/java/org/springframework/cache/annotation/ReactiveCachingTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.params.provider.ValueSource;
2727
import reactor.core.publisher.Flux;
2828
import reactor.core.publisher.Mono;
29+
import reactor.test.StepVerifier;
2930

3031
import org.springframework.cache.Cache;
3132
import org.springframework.cache.CacheManager;
@@ -145,6 +146,23 @@ void cacheErrorHandlerWithLoggingCacheErrorHandler() {
145146
assertThat(r1).as("cacheFlux blockFirst").isEqualTo(2L);
146147
}
147148

149+
@Test
150+
void cacheErrorHandlerWithLoggingCacheErrorHandlerAndMethodError() {
151+
AnnotationConfigApplicationContext ctx =
152+
new AnnotationConfigApplicationContext(ExceptionCacheManager.class, ReactiveFailureCacheableService.class, ErrorHandlerCachingConfiguration.class);
153+
ReactiveCacheableService service = ctx.getBean(ReactiveCacheableService.class);
154+
155+
Object key = new Object();
156+
StepVerifier.create(service.cacheMono(key))
157+
.expectErrorMessage("mono service error")
158+
.verify();
159+
160+
key = new Object();
161+
StepVerifier.create(service.cacheFlux(key))
162+
.expectErrorMessage("flux service error")
163+
.verify();
164+
}
165+
148166
@Test
149167
void cacheErrorHandlerWithSimpleCacheErrorHandler() {
150168
AnnotationConfigApplicationContext ctx =
@@ -214,6 +232,20 @@ Flux<Long> cacheFlux(Object arg) {
214232
}
215233
}
216234

235+
@CacheConfig(cacheNames = "first")
236+
static class ReactiveFailureCacheableService extends ReactiveCacheableService {
237+
238+
@Cacheable
239+
Mono<Long> cacheMono(Object arg) {
240+
return Mono.error(new IllegalStateException("mono service error"));
241+
}
242+
243+
@Cacheable
244+
Flux<Long> cacheFlux(Object arg) {
245+
return Flux.error(new IllegalStateException("flux service error"));
246+
}
247+
}
248+
217249

218250
@Configuration(proxyBeanMethods = false)
219251
@EnableCaching

0 commit comments

Comments
 (0)