1

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?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
sferencik
  • 3,144
  • 1
  • 24
  • 36
  • 4
    This looks like pretty sloppy code to be honest. It's eating all exceptions, regardless of origin, and then blowing up in a different way. It'd be better to interpret specific exceptions and show a more meaningful error. – tadman Oct 26 '17 at 18:54
  • 2
    I would consider this being a code smell... – spickermann Oct 26 '17 at 18:56
  • As *tadman* mentioned, `abort` should provide a meaningful message. See, for example: [Write a simple, clean error message without a backtrace and exit on failure - Code Review Stack Exchange](https://codereview.stackexchange.com/questions/285164/write-a-simple-clean-error-message-without-a-backtrace-and-exit-on-failure) – Timur Shtatland May 25 '23 at 18:41

0 Answers0