如何从Ruby脚本和捕获错误
问题描述:
我有一个错误,有时运行bundle install
如何从Ruby脚本和捕获错误
Bundler::GemspecError: Could not read gem at /path/to/cache/gem It may be corrupted
时,我有一个网页的URL过滤器,似乎在下载宝石,如果我删除(阻止初次尝试运行包有问题的缓存文件并再次运行,它工作)。此外,缓存文件的内容是来自Web网页过滤器页面的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/[email protected]/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世界之外运行捆绑软件可以这么说。
任何人都可以提供如何我会去了解这个请
感谢
答
您不能从系统营救的命令,因为它不是为红宝石东西致命应的命令失败,而是你有任何意见使用if条件检查系统命令是成功还是失败。 它会更喜欢:
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
的可能的复制[红宝石系统命令检查退出代码(https://stackoverflow.com/questions/18728069/ruby-system-command-check-exit-code) –