Last active
December 14, 2015 01:58
-
-
Save ymasory/5009871 to your computer and use it in GitHub Desktop.
Fail fast with Typesafe Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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