Apparently `loadConfig()` fails if there is nothin...
# hoplite
s
Apparently
loadConfig()
fails if there is nothing to load from any configured property source. I'd like to fall back to some default configuration on that kind of "failure". However, I still want to capture "real" failures, like syntax errors in configuration files. What's the correct way to distinguish "no config available" from "config broken" in Hoplite?
c
Perhaps provide a default config to entirely avoid “no config available”
s
But the default config should get overridden by any provided config. So also with a default config "beforehand" instead of a fallback, I still need to be able to determine whether configuration providers actually provide something.
It seems that usually, I could check for
ConfigFailure.UndefinedTree
. But that does not work anymore as soon as I add
.addEnvironmentSource()
. Then an empty configuration suddenly causes
ConfigFailure.MissingConfigValue
.
d
It crashes even if you provide default values in all your configs' data classes?
s
Yes.
d
🤔 I don't know if this helps, but I never really encounter these issues, since most my projects are migrated from Micronaut so I set up hoplite like they load configs:
Copy code
ConfigLoaderBuilder.default().apply {
        addDecoder(IntRangeDecoder())
        addEnvironmentSource()
        val envs = System.getenv("MICRONAUT_ENVIRONMENTS")?.split(',') ?: emptyList()
        envs.reversed().forEach {
            addResourceSource("/application-$it.yaml", optional = true)
        }
        addResourceSource("/application.yaml")
    }.build().loadConfigOrThrow<Settings>()
Then I have a base settings file for all run types, and one for local, one for testing, and dev/staging/production environments.
I then just use env vars to pick which one.
The later the environment name in the env var, the more priority it has in overriding settings. ( The last resource listed is the one overridden by the ones before it.
s
So, are you saying that your code does not throw if none of your configured property sources contains any configuration?
d
No, what I'm saying is that application.yaml always contains common configs for all environments, and then I just specify the missing ones using default values on the config classes or overriding/providing them per environment I run in... thereby avoiding the problem completely... I'm really wondering in what case no configs is really valid anyways... there's always going to be defaults/common configs to provide.
But then, I may not be understanding your use case..., also it's good to know about that
optional
parameter in
addResourceSource
... then if that file doesn't exist, it won't crash.
r
It crashes even if you provide default values in all your configs' data classes?
This particular case should be fixed with https://github.com/sksamuel/hoplite/pull/486.
👍🏻 1