How can you cleanly require an application variable to be set?

I’m currently using Application.get_env/3 to get some configuration in my Phoenix router. However, I just realized that the key was set incorrectly and it’s been returning nil without me realizing.

Currently the code looks like this:

api_hostname = Application.get_env(:web_interface, :api_hostname)

Ideally there would be a Application.get_env!/3 that would raise an error when the key is not set, but since there is not what would be the cleanest way to accomplish this goal?

Here are some IMO ugly ways:

api_hostname = case Application.get_env(:web_interface, :api_hostname) do
  nil -> raise "Variable not set"
  var -> var

Nevermind I see that I can use Application.fetch_env!/2. I’ll probably use that for the majority of my config variables now, especially in something like a Phoenix Router.

1 Like