Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve config locations error messages when resource not found #901

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ protected List<ConfigSource> loadConfigSources(final String[] locations, final i
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
final List<ConfigSource> configSources = new ArrayList<>();
final Path urlPath = uri.getScheme() != null ? Paths.get(uri) : Paths.get(uri.getPath());
if (Files.isRegularFile(urlPath)) {
if (Files.isRegularFile(urlPath) || "file".equals(uri.getScheme())) {
consumeAsPath(toURL(urlPath.toUri()), new ConfigSourcePathConsumer(ordinal, configSources));
} else if (Files.isDirectory(urlPath)) {
try (DirectoryStream<Path> paths = Files.newDirectoryStream(urlPath, this::validExtension)) {
for (Path path : paths) {
addConfigSource(path.toUri(), ordinal, configSources);
}
} catch (IOException e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, uri.toString());
}
}
return configSources;
Expand All @@ -125,7 +125,7 @@ protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, fina
try {
consumeAsPaths(useClassloader, uri.getPath(), new ConfigSourcePathConsumer(ordinal, configSources));
} catch (IOException e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, uri.toString());
} catch (IllegalArgumentException e) {
configSources.addAll(fallbackToUnknownProtocol(uri, ordinal, useClassloader));
}
Expand All @@ -137,7 +137,7 @@ protected List<ConfigSource> tryJar(final URI uri, final int ordinal) {
try {
consumeAsPath(toURL(uri), new ConfigSourcePathConsumer(ordinal, configSources));
} catch (Exception e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, uri.toString());
}
return configSources;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ protected List<ConfigSource> fallbackToUnknownProtocol(final URI uri, final int
}
}
} catch (IOException e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, uri.toString());
}
return configSources;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ private ConfigSource addConfigSource(final URL url, final int ordinal, final Lis
configSources.add(configSource);
return configSource;
} catch (IOException e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, url.toString());
}
}

Expand All @@ -235,7 +235,7 @@ private void addProfileConfigSource(final URL profileToFileName, final int ordin
} catch (FileNotFoundException | NoSuchFileException e) {
// It is ok to not find the resource here, because it is an optional profile resource.
} catch (IOException e) {
throw ConfigMessages.msg.failedToLoadResource(e);
throw ConfigMessages.msg.failedToLoadResource(e, profileToFileName.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public interface ConfigMessages {
@Message(id = 34, value = "URI Syntax invalid %s")
IllegalArgumentException uriSyntaxInvalid(@Cause Throwable cause, String uri);

@Message(id = 35, value = "Failed to load resource")
IllegalStateException failedToLoadResource(@Cause Throwable cause);
@Message(id = 35, value = "Failed to load resource %s")
IllegalStateException failedToLoadResource(@Cause Throwable cause, String location);

@Message(id = 36, value = "Type %s not supported for unwrapping.")
IllegalArgumentException getTypeNotSupportedForUnwrapping(Class<?> type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ void notFound() {

assertNull(config.getRawValue("my.prop"));
assertEquals(0, countSources(config));

assertThrows(IllegalStateException.class, () -> buildConfig("file:/not/found/not-found.properties"),
"Failed to load resource file:/not/found/not-found.properties");
assertThrows(IllegalStateException.class, () -> buildConfig("http://not.found/not-found-properties"),
"Failed to load resource http://not.found/not-found-properties");
assertThrows(IllegalStateException.class, () -> buildConfig("jar:file:/resources-one.jar!/"),
"Failed to load resource jar:file:/resources-one.jar!/");
}

@Test
Expand Down Expand Up @@ -175,14 +182,13 @@ void onlyProfileFile(@TempDir Path tempDir) throws Exception {
devProperties.store(out, null);
}

SmallRyeConfig config = new SmallRyeConfigBuilder()
SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder()
.addDiscoveredSources()
.addDefaultInterceptors()
.withProfile("common,dev")
.withDefaultValue(SMALLRYE_CONFIG_LOCATIONS, tempDir.resolve("config.properties").toUri().toString())
.build();
.withDefaultValue(SMALLRYE_CONFIG_LOCATIONS, tempDir.resolve("config.properties").toUri().toString());

assertNull(config.getRawValue("my.prop.profile"));
assertThrows(IllegalStateException.class, builder::build);
}

@Test
Expand Down