Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

ruby - How to rescue from a require "gem_name" when the gem is not installed

I'm writing a library that depend on a specific gem. I require the gem and use it in my code and everything is hunky-dory as long as the gem is installed on the user's machine. but what if it is not?!

I thought it's fine cause I can rescue from the require command and print a message to the output to inform the user about the lacking gem and finish it gracefully but I get an error!

Could you tell me how it should be done or what is wrong with this piece of code:

begin 
 require "some_gem"
rescue
 puts "please install some_gem first!" 
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

require raises a LoadError exception if it can't load the required library. However, you never rescue from LoadError anywhere, you rescue from StandardError.

If you want to rescue from LoadError, you have to say so:

begin 
  require 'some_gem'
rescue LoadError
  puts 'please install some_gem first!'
end

Even better yet, make sure that you are actually printing the correct missing dependency:

begin 
  require 'some_gem'
rescue LoadError => e
  raise unless e.message =~ /some_gem/
  puts 'please install some_gem first!'
end

(This re-raises the exact same exception that was rescued from, in case that the exception was actually caused by some other missing library somewhere else. You wouldn't want to print misleading information, right?)

Depending on what the intended target audience for the library is and whether or not they might be scared away by a backtrace being dumped to their console, you might want to re-raise the exception in any case, instead of just swallowing it:

begin 
  require 'some_gem'
rescue LoadError => e
  puts 'please install some_gem first!' if e.message =~ /some_gem/
  raise
end

Or, you could skip the puts and instead raise an exception with the message set to what you want to say:

begin 
  require 'some_gem'
rescue LoadError => e
  raise e.exception('please install some_gem first!') if e.message =~ /some_gem/
  raise
end

Except now the exception is raised in the wrong place and thus has the wrong line number and stacktrace and thus is misleading, but that is easily fixed:

begin 
  require 'some_gem'
rescue LoadError => e
  raise unless e.message =~ /some_gem/
  friendly_ex = e.exception('please install some_gem first!')
  friendly_ex.set_backtrace(e.backtrace)
  raise friendly_ex
end

Now you print pretty much the same thing that you would have printed with the puts, but you have a "proper" exception that for example allows better debugging or allows a consumer of your library to rescue that exception and handle it their way, both of which would have been impossible or at least hard with your solution that just swallows the exception.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...