如何从ruby脚本运行bundle并捕获错误

时间:2017-08-10 08:22:31

标签: ruby

运行bundle install

时有时会出错
Bundler::GemspecError: Could not read gem at /path/to/cache/gem It may be corrupted

我有一个web url过滤器似乎阻止了下载gem的初始尝试(如果我删除有问题的缓存文件并再次运行,它可以工作)。此外,缓存文件的内容是来自web url过滤器页面的html。

我不想删除缓存文件并重新运行,如果发生这种情况,我希望自动重新运行捆绑包。

我曾考虑过从ruby脚本运行bundle,但我似乎无法捕获错误。

我需要在docker中自动构建项目。

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

然而,我似乎无法拯救这个例外;抛出的错误是:

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.

未捕获异常,因为我没有输出。我被告知这是因为我现在正在Ruby世界之外运行bundle。可以这么说。

任何人都可以就如何解决这个问题提出任何建议

由于

1 个答案:

答案 0 :(得分:0)

你不能从系统命令中解救,因为如果命令失败,它不会对ruby造成致命的伤害,而是你必须使用if条件检查系统命令是成功还是失败。 这更像是:

dev