Skip to content

Commit

Permalink
Improve error message when spring.config.import fails to resolve
Browse files Browse the repository at this point in the history
Update `StandardConfigDataLocationResolver` to give a better error
message when a location cannot be resolved. Prior to this commit, a
location with a misspelling in the prefix would only give an error
about the file extension being not known.

Fixes gh-36243
  • Loading branch information
philwebb committed Jun 26, 2024
1 parent 85f6641 commit 8bcdb4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
Expand Down Expand Up @@ -69,17 +69,17 @@ class ConfigDataLocationResolvers {
@SuppressWarnings("rawtypes")
private List<ConfigDataLocationResolver<?>> reorder(List<ConfigDataLocationResolver> resolvers) {
List<ConfigDataLocationResolver<?>> reordered = new ArrayList<>(resolvers.size());
StandardConfigDataLocationResolver resourceResolver = null;
ConfigDataLocationResolver<?> standardConfigDataLocationResolver = null;
for (ConfigDataLocationResolver<?> resolver : resolvers) {
if (resolver instanceof StandardConfigDataLocationResolver configDataLocationResolver) {
resourceResolver = configDataLocationResolver;
if (resolver instanceof StandardConfigDataLocationResolver) {
standardConfigDataLocationResolver = resolver;
}
else {
reordered.add(resolver);
}
}
if (resourceResolver != null) {
reordered.add(resourceResolver);
if (standardConfigDataLocationResolver != null) {
reordered.add(standardConfigDataLocationResolver);
}
return Collections.unmodifiableList(reordered);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -231,8 +232,17 @@ private Set<StandardConfigDataReference> getReferencesForFile(ConfigDataLocation
if (configDataLocation.isOptional()) {
return Collections.emptySet();
}
throw new IllegalStateException("File extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator");
if (configDataLocation.hasPrefix(PREFIX) || configDataLocation.hasPrefix(ResourceUtils.FILE_URL_PREFIX)
|| configDataLocation.hasPrefix(ResourceUtils.CLASSPATH_URL_PREFIX)
|| configDataLocation.toString().indexOf(':') == -1) {
throw new IllegalStateException("File extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator");
}
throw new IllegalStateException(
"Incorrect ConfigDataLocationResolver chosen or file extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator. "
+ "The location is being resolved using the StandardConfigDataLocationResolver, "
+ "check the location prefix if a different resolver is expected");
}

private String getLoadableFileExtension(PropertySourceLoader loader, String file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
Expand Down Expand Up @@ -99,6 +99,16 @@ void resolveWhenLocationIsFileAndNoMatchingLoaderThrowsException() {
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageStartingWith("File extension is not known"));
}

@Test
void resolveWhenLocationHasUnknownPrefixAndNoMatchingLoaderThrowsException() {
ConfigDataLocation location = ConfigDataLocation
.of("typo:src/test/resources/configdata/properties/application.unknown");
assertThatIllegalStateException().isThrownBy(() -> this.resolver.resolve(this.context, location))
.withMessageStartingWith("Unable to load config data from")
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageStartingWith(
"Incorrect ConfigDataLocationResolver chosen or file extension is not known to any PropertySourceLoader"));
}

@Test
void resolveWhenLocationWildcardIsSpecifiedForClasspathLocationThrowsException() {
ConfigDataLocation location = ConfigDataLocation.of("classpath*:application.properties");
Expand Down

0 comments on commit 8bcdb4b

Please sign in to comment.