Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rubygems

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
like image 689
Allen Bargi Avatar asked Jan 14 '10 01:01

Allen Bargi


2 Answers

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.

like image 154
Jörg W Mittag Avatar answered Sep 28 '22 01:09

Jörg W Mittag


begin
  require "some_gem"
rescue LoadError
  puts "message"
end
like image 28
nitecoder Avatar answered Sep 28 '22 01:09

nitecoder