scalatypesafe-config

How to avoid com.typesafe.config.ConfigException$Missing in Scala


I have a Scala project and I am reading config file to retrive value of constant from application.conf.

val config = ConfigFactory.load()

val param = config.getString("project.constant"+args(1))

Now if the constant is not present, then param should take a default value.

But the problem is if the constant is not present in application.conf, it gives runtime exception com.typesafe.config.ConfigException$Missing .

I have written a function

def isEmpty(x:String) = Option(x).forall(_.isEmpty)

so I can use isEmpty(param) to determine that if it is null, assign a default value.

But the runtime exception occurs at config.getString().


Solution

  • I found the answer, we can use the method hasPath() to determine if a given property is defined within a given config or not.