0

I have an error sometimes when running bundle install

Bundler::GemspecError: Could not read gem at /path/to/cache/gem It may be corrupted

I have a web URL filter that seems to block the initial attempt at downloading the gem (if I delete the cache file in question and run again, it works). Furthermore, the contents of the cache file is the html from the web URL filter page.

I would like to not have to delete the cache file and re-run, I would like to have bundler automatically rerun if this scenario happens.

I have thought about running bundle from a Ruby script, but I cannot seem to capture the error.

I need to automate the build of my project in Docker.

begin
  puts "Starting bundle install"
  system %w[bundle install]
rescue Bundler::GemspecError => e
  puts e
end

However, I cannot seem to rescue the exception; the error thrown is:

Bundler::GemspecError: Could not read gem at /Users/lewis/.rvm/gems/ruby-2.3.3@hendricks-offline/cache/rack-2.0.3.gem. It may be corrupted.
An error occurred while installing rack (2.0.3), and Bundler cannot continue.
Make sure that `gem install rack -v '2.0.3'` succeeds before bundling.

The exception is not captured, as I get no output. I have been advised that this is because i am running bundle now outside of Ruby world, so to speak.

How would I go about this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Richlewis
  • 15,070
  • 37
  • 122
  • 283
  • 1
    Possible duplicate of [ruby system command check exit code](https://stackoverflow.com/questions/18728069/ruby-system-command-check-exit-code) – Brad Werth Aug 10 '17 at 08:26

1 Answers1

0

You can't rescue from system commands as it's not something fatal for ruby should the command fail, instead you have to check if the system command succeeded or failed using an if condition. It would be more like:

puts "Starting bundle install"
if system('bundle install')
  puts 'bundle successful'
else
  puts 'bundle failed, deleting cache file and retrying'
  system('command to delete cache file goes here')
  system('bundle install')
end
Thermatix
  • 2,757
  • 21
  • 51