System.get_env!/1 that raises if the ENV var is not present

303 views
Skip to first unread message

Paul Smith

unread,
Nov 4, 2015, 10:45:27 AM11/4/15
to elixir-lang-core
In almost all cases I would like to treat a missing ENV var as an error and blow up immediately so I know what's wrong. The current behavior of `System.get_env/1` returns nil, which isn't very helpful and can be hard to trace. Having a version that raises would make debugging a lot easier. Right now I created my own function in the app that calls System.get_env and raises if the value is nil, I think this isn't very ideal.

I'd be happy to submit a PR if this is something that should be in Elixir!

- Paul

Theron Boerner

unread,
Nov 4, 2015, 10:54:26 AM11/4/15
to elixir-l...@googlegroups.com
I would prefer if it returned an {:ok, _} or {:error, _} tuple, though I don't know about everyone else.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/ec5f75cb-5d2e-4247-91be-934caf049b03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Valim

unread,
Nov 4, 2015, 10:58:41 AM11/4/15
to elixir-l...@googlegroups.com
System.get_env only returns a string or nil, so it is easily distinguishable without a need for a tuple. The advantage of returning nil is that || can be easily used to provide a default value, which is a common thing to do.



José Valim
Skype: jv.ptec
Founder and Director of R&D

Paul Smith (thoughtbot)

unread,
Nov 4, 2015, 11:01:33 AM11/4/15
to elixir-l...@googlegroups.com
Right, I can definitely see the advantage of returning nil, so I’m suggesting adding a new function that raises (hard to see the ! in the subject).

Would you be up for adding a new function that raises?

You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/yw54VGwGkt4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B33q7qEWaBzRwqBn4Wz5KLrtv145Tebxxpm4%2Bb6JUBmA%40mail.gmail.com.

Paul Smith

unread,
Nov 4, 2015, 11:04:20 AM11/4/15
to elixir-lang-core
Maybe a better name would be `System.fetch_env!/1` to match the Dict module a bit better?

Peter Hamilton

unread,
Nov 4, 2015, 11:13:39 AM11/4/15
to elixir-lang-core

How about we make System.env a Dict, so you can do: Dict.fetch!(System.env, :foo)


On Wed, Nov 4, 2015, 8:04 AM Paul Smith <pa...@thoughtbot.com> wrote:
Maybe a better name would be `System.fetch_env!/1` to match the Dict module a bit better?

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Paul Smith

unread,
Nov 4, 2015, 11:20:15 AM11/4/15
to elixir-lang-core
You bring up a good point. That might be the best solution, which is great because you can already do that!

Dict.fetch!(System.get_env, "MY_VAR") 

This might be better than adding a new function that basically does exactly this

Ben Wilson

unread,
Nov 4, 2015, 12:57:33 PM11/4/15
to elixir-lang-core
Yeah I like the pattern of
System.get_env |> Map.fetch!("MY_ENV_VAR")

a lot better than a new function that raises by default

Wojtek Mach

unread,
Jan 2, 2016, 1:48:40 AM1/2/16
to elixir-lang-core
Hi all,

System.get_env |> Map.fetch!("FOO")

does the trick, however when it raises an error it puts the entire ENV into the exception message which may or may not be a good thing. I guess locally it's not a big deal and it helps debugging however I'd be afraid of exposing the contents of ENV (which often has some secrets) in a CI/prod. Again, perhaps it's not a big concern.

I've been using following code on my projects:
secret_key_base: System.get_env("SECRET_KEY_BASE") || raise("ENV variable not set: SECRET_KEY_BASE")

It's a bit tedious to write it down every single time, but that's OK. Still, I think System.get_env!/1 or System.fetch_env!/1 would be a good addition to the stdlib and would encourage folks to "fail fast" when the application boots.

Paul Smith

unread,
Jan 2, 2016, 10:26:00 AM1/2/16
to elixir-lang-core
That's a good point. I've also noticed that System.get_env sometimes picks ENV variables up that Map.fetch! does not. So sometimes I have to do what you're doing with the raise.

Now that I've tried the Map.fetch! Way of doing things and it doesn't work that well, I think it would be nice to have System.get_env! added. What does everyone else think?

Either way it would probably be good to make System.get_env work more reliably so you can use Map.fetch! Maybe what's happening now is a bug? I'll open up an issue about that next week just in case.

Matthew Wean

unread,
Dec 29, 2016, 4:15:37 PM12/29/16
to elixir-lang-core
I would love to have a raising version too. As it stands, I often get confusing errors deep in the system because of missing ENV variables. Is this something we can add?

José Valim

unread,
Dec 29, 2016, 4:58:14 PM12/29/16
to elixir-l...@googlegroups.com
Please open up an issue. We just need to figure out if the name will be get_env! or fetch_env! (I could argue any).



José Valim
Skype: jv.ptec
Founder and Director of R&D

On Thu, Dec 29, 2016 at 10:15 PM, Matthew Wean <matthe...@gmail.com> wrote:
I would love to have a raising version too. As it stands, I often get confusing errors deep in the system because of missing ENV variables. Is this something we can add?

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f66bbf70-e99d-48ef-8c4c-d1bfae031c88%40googlegroups.com.

Michał Muskała

unread,
Dec 29, 2016, 5:08:55 PM12/29/16
to elixir-l...@googlegroups.com
I guess the standard set of get + fetch + fetch! would be the best solution. It would be the least surprising since it works like this nearly everywhere.

Michał.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KG2Ws1E9Yc63_5%3DB%3DT4t5DC5pp3uPAfS07Ta6SebQ%3DxQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages