Ruby, which exception is best to handle unset environment variables?
Asked Answered
T

6

9

The script I wrote runs at start up and requires that an environment variable be set, but which of Ruby's Exceptions, is best? I used LoadError, I just want to be as descriptive as possible and follow the proper conventions.

Secondly, I can't find another way to see if an environment variable is set besides checking it's length, but that doesn't seem so elegant.

begin
  raise LoadError if ENV['FOO'].to_s.length == 0
  system "open http://example.com/" + ENV['FOO']
rescue Exception => e
  puts "=> #{e} FOO environment variable not set"
end
Translucent answered 12/8, 2012 at 0:53 Comment(2)
any reason to not just write your own exception?Phlebotomy
Because I'd rather follow a convention that is more popular if it's available to me.Translucent
W
6

Could use ENV.fetch('FOO') which then raises a KeyError if not found.

Wilser answered 17/12, 2019 at 19:32 Comment(1)
This is the most simple and straight forward, since 2012 things have changed and this seems to have emerged as a popular design pattern.Translucent
N
14

You can do something like:

ENV['SECRET_KEY_XXYY'] || raise('no SECRET_KEY_XXYY provided')
Nonpayment answered 5/2, 2014 at 23:15 Comment(0)
A
8

According to the documentation for LoadError that is supposed to be used for when a 'require' has an issue. I think the more proper method would be to subclass StandardError and make one that fits your use. If that seems a bit much I would just go with StandardError with a descriptive message.

Android answered 12/8, 2012 at 1:4 Comment(0)
W
6

Could use ENV.fetch('FOO') which then raises a KeyError if not found.

Wilser answered 17/12, 2019 at 19:32 Comment(1)
This is the most simple and straight forward, since 2012 things have changed and this seems to have emerged as a popular design pattern.Translucent
S
3

Making your own exceptions is easy:

MyError = Class.new(StandardError)
raise MyError, "FOO environment variable not set" unless ENV['FOO']
system "open http://example.com/" + ENV['FOO']

Catching the exception in that code block may not be appropriate in this case, since it seems you are just printing a message with it. As a rule, never raise an exception unless you are prepared for it to terminate the program. In other words, avoid using exceptions for expected conditions. If the program can continue without FOO being set, it would be better to simply make execution of the system statement conditional:

system("open http://example.com/" + ENV['FOO']) if ENV['FOO']

or

ENV['FOO'] && system("open http://example.com/" + ENV['FOO'])
Sigismond answered 12/8, 2012 at 3:55 Comment(0)
D
3

There's a list of exception types at http://bugs.ruby-lang.org/projects/ruby/wiki/ExceptionClassesDoc

I'd probably choose ArgumentError , as you're saying that the value of ENV['FOO'] isn't what you expected it to be.

Delphiadelphic answered 12/8, 2012 at 23:20 Comment(0)
E
1

You could use fetch with a block to raise specific error

ENV.fetch('FOO') { raise 'FOO environment variable is not set' }
Ess answered 2/3, 2023 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.