Ruby on Rails saving from Errno :: ENOENT

I need to save from "Errno :: ENOENT" in a Ruby on Rails 3.0.4 application. I currently have a controller with the following code:

begin `lame #{parameters}` rescue Errno::ENOENT logger.info "command 'lame' not found: ensure LAME is installed" end 

However, the log code is never called, but the logs show:

script / rails: No such file or directory - lame ...

If I create a ruby ​​script with the same fragment, the exception will be saved.

+6
ruby ruby-on-rails
source share
1 answer

In Ruby 1.8, Errno::ENOENT does not occur when executing a shell / reverse tick - the error you see is a standard error printed by the shell. If you want to detect this, I would recommend looking for exit code 127:

 `lame #{parameters} 2>&1` if $?.exitstatus == 127 logger.info "command 'lame' not found: ensure LAME is installed" end 

However, this will lead to an increase in Errno::ENOENT in Ruby 1.9.

Instead, you can check the output of which lame :

 lame_installed = system("which lame >/dev/null") # or even better lame_available = !(lame_path = `which lame`.strip).empty? && File.executable?(lame_path) 

Further reading:

+5
source share

All Articles