Skip to content

Instantly share code, notes, and snippets.

@ymasory
Last active December 14, 2015 01:58
Show Gist options
  • Save ymasory/5009871 to your computer and use it in GitHub Desktop.
Save ymasory/5009871 to your computer and use it in GitHub Desktop.
Fail fast with Typesafe Config
import com.typesafe.config.{ Config, ConfigFactory, ConfigException }
import scala.util.control.Exception._
/* At the beginning of app execution load essential configurations and assign
* them to vals. */
object MyAppConfig {
private[this] val conf: Config = trapConfig {
ConfigFactory.load() getConfig "myapp"
}
/* the actual configurations are assigned here */
val port = trapConfig { conf getInt "port" }
val interface = trapConfig { conf getString "interface" }
/* Improves error messages on missing configurations, before exiting.
* Nothing more. */
private[this] def trapConfig[A](block: => A): A =
catching(classOf[ConfigException.Missing]) withApply { e =>
log error {
s"""|
|###############################################
| MyApp
|
|Could not initialize configurations.
|You are probably missing a -Doption to the JVM.
|Or perhaps application.conf isn't in classpath?
|
|${e.getMessage}
|###############################################""".stripMargin
}
sys exit 1
} apply block
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment