Stop wrapping low-level exceptions in CacheAspectSupport initialization

This commit replaces the IllegalStateException thrown in
CacheAspectSupport when a CacheManager cannot be determined. These were
added to provide a dedicated error message, but it is possible to do
so without hiding the more adequate exception type.

Closes gh-22442
This commit is contained in:
Stéphane Nicoll 2024-04-29 11:48:52 +02:00
parent 9b85c93b6b
commit 9492d88270
3 changed files with 25 additions and 17 deletions

View File

@ -20,6 +20,8 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
@ -91,8 +93,9 @@ class AspectJEnableCachingIsolatedTests {
try {
load(MultiCacheManagerConfig.class);
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage()).contains("bean of type CacheManager");
catch (NoUniqueBeanDefinitionException ex) {
assertThat(ex.getMessage()).contains(
"no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]");
}
}
@ -116,8 +119,8 @@ class AspectJEnableCachingIsolatedTests {
try {
load(EmptyConfig.class);
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage()).contains("no bean of type CacheManager");
catch (NoSuchBeanDefinitionException ex) {
assertThat(ex.getMessage()).contains("no CacheResolver specified");
}
}

View File

@ -270,12 +270,17 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
setCacheManager(this.beanFactory.getBean(CacheManager.class));
}
catch (NoUniqueBeanDefinitionException ex) {
throw new IllegalStateException("No CacheResolver specified, and no unique bean of type " +
"CacheManager found. Mark one as primary or declare a specific CacheManager to use.", ex);
StringBuilder message = new StringBuilder("no CacheResolver specified and expected a single CacheManager bean, but found ");
message.append(ex.getNumberOfBeansFound());
if (ex.getBeanNamesFound() != null) {
message.append(": [").append(StringUtils.collectionToCommaDelimitedString(ex.getBeanNamesFound())).append("]");
}
message.append(" - mark one as primary or declare a specific CacheManager to use.");
throw new NoUniqueBeanDefinitionException(CacheManager.class, ex.getNumberOfBeansFound(), message.toString());
}
catch (NoSuchBeanDefinitionException ex) {
throw new IllegalStateException("No CacheResolver specified, and no bean of type CacheManager found. " +
"Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.", ex);
throw new NoSuchBeanDefinitionException(CacheManager.class, "no CacheResolver specified - "
+ "register a CacheManager bean or remove the @EnableCaching annotation from your configuration.");
}
}
this.initialized = true;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -89,13 +89,12 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
@Test
void multipleCacheManagerBeans() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MultiCacheManagerConfig.class);
assertThatThrownBy(ctx::refresh)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("no unique bean of type CacheManager")
.hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
.isInstanceOf(NoUniqueBeanDefinitionException.class)
.hasMessageContaining("no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]")
.hasNoCause();
}
@Test
@ -117,13 +116,14 @@ class EnableCachingTests extends AbstractCacheAnnotationTests {
@Test
void noCacheManagerBeans() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(EmptyConfig.class);
assertThatThrownBy(ctx::refresh)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("no bean of type CacheManager")
.hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
.isInstanceOf(NoSuchBeanDefinitionException.class)
.hasMessageContaining("no CacheResolver specified")
.hasMessageContaining(
"register a CacheManager bean or remove the @EnableCaching annotation from your configuration.")
.hasNoCause();
}
@Test