Ruby rescue and abort - ruby

I am seeing this pattern in someone else's Rakefile:
begin
sh "..."
rescue
abort
end
What's the author doing here? Why rescue if I'm going to abort anyway? Do I understand correctly that this mutes the potential shell error?
I am new to ruby and rake. As far as I understand, the sh is rake's shorthand for FileUtils.sh() (ref). And FileUtils.sh() may raise a RuntimeError (ref).
Does this mean that we are effectively throwing away the information/message in the RuntimeError and exiting rake with an error status, but without a specific error message? Is this a rake/ruby pattern? Any recommended reading?

Related

Capture failures when using Ruby open3 standard library

I'm running an external command using the Open3.popen2e function. The external command fails spectacularly halfway through. Unfortunately this also kills my ruby process that is using popen2e. What is the reason, and how can I avoid?
begin
Open3.popen2e("node mynode.js") do |i, oe|
oe.each do |ln|
puts ln.chomp
end
end
rescue => exception
puts exception.message
end
Using ruby 2.5.1 on Ubuntu
Not sure about the reason, but a non-StandardError may be raised. So, what about changing your rescue to:
rescue Exception => exception
That may give you more clues as to what is happening.

Escape Mechinze Error in Ruby Scraping

I am getting the following server response error while trying to scrape SERP results:
/Users/*********/.rvm/gems/ruby-2.3.0/gems/mechanize-2.7.5/lib/mechanize/http/agent.rb:323:in `fetch': 503 => Net::HTTPServiceUnavailable for http://******.*****.com/sorry/index?continue=http://www.********.com/search%3Fq%3D<term1>%2B<term2> -- unhandled response (Mechanize::ResponseCodeError)
I am trying to figure out how to escape the error / exception, so that the program will continue to run instead of automatically exiting.
Like anything in Ruby it probably boils down to rescue and recover:
loop do
begin
Mechanize.do_stuff!
# Success!
break
rescue Mechanize::ResponseCodeError
# Server-side failure, so let's try again after a quick break
sleep(10)
end
end
Note the sleep(10) is there to avoid slamming the server furiously and making it malfunction even harder.

Using single line conditional with require/rescue

I want to avoid an error, if a require is not successfull.
I can do it with:
begin
require 'unexisting_script'
rescue LoadError
end
I tried to do the same with a one-line condition:
require 'unexisting_script' rescue LoadError
and get the error no such file to load -- unexisting_script (LoadError)
With other exceptions/commands I have no problem with a one line rescue, this works:
1 / 0 rescue ZeroDivisionError
I also tried to bracket the command, but withous success:
(require 'unexisting_script') rescue LoadError
I can put everything in one line with ;:
begin require 'unexisting_script'; rescue LoadError; end
but I'm still wondering, why the shortest version does not work.
I found some related questions, but none of them is mentioning a problem with require and rescue:
Why does this rescue syntax work?
How does one use rescue in Ruby without the begin and end block
My question:
Can I use rescue in a one-line condition with require? If yes: how? If no: Why?
You cannot specify the error class when you use rescue in postfix/oneliner notation. What rescue LoadError or rescue ZeroDivisionError means is that it will rescue a (subclass of) StandardError, and in such case, evaluate LoadError or ZeroDivisionError, which has no effect. Since ZeroDivisionError is a subclass of StandardError, it was captured, but LoadError is not, and it was not captured.
By the way, I cannot think of a use case where you want to not raise an error when a required file does not exist. Required files are dependencies, and if requiring them fails, then the program will not work correctly anyway. I feel a code smell in what you are doing. When failure of loading a file does not mess the program, that is usually when you should be using load instead of require.

Rake equivalent to make -k (--keep-going)

By default Rake will stop at the first exception raised.
There doesn't seem to be a command line equivalent to make -k, is there any way to do it programmaticaly?
Unfortunately there is no --keep-going in rake (and I think that's fine). The only way I can think of is to wrap your Rakefile with begin; rescue; end, but it won't make it "keep going" -- it will simply ensure that your rake's execution return is 0.
begin
# Former Rakefile code goes here
rescue
puts $!.inspect
end
I believe that "keep going" isn't possible since when you raise something you already changed your algorithm's flow -- i.e. don't try to solve your problem by ignoring Exceptions.

Rescue RuntimeError in Rake

I have a custom Rakefile which calls different file tasks. Sometimes a file that is expected doesn't exist, and rake throws a RuntimeError and fails. However, I'd like to do a few things before it fails. So is there any way I could rescue a RuntimeError? Or is there some sort of a magic task which gets called before a complete fail?
I haven't run into this issue with rake myself, but you could try simply wrapping your call to the file tasks in a begin-rescue block, i.e.
begin
file_task
rescue RuntimeError => e
puts e
end
and then do your rescuing in the rescue block.

Resources