我想趕上PuppetError ::錯誤例外,所以它不是致命的,但PuppetError :: ExecError應該是致命的。如何搶救紅寶石異常,但不是失敗的代碼塊
我的問題似乎是,救援子句捕捉PuppetError ::錯誤,那麼它提升到ExecError狀態,所以它不會被調用代碼捕獲。我需要將救援條款限制在一條線上,即Open3 ..線。但是如何?
module PuppetCert
def self.remove(instance_hostname)
begin
Open3::popen3("puppet cert clean #{instance_hostname}") do |stdin, stdout, stderr, wait_thr|
if wait_thr.value.success?
puts "success"
else
raise PuppetError::Error
return
end
end
rescue
raise PuppetError::ExecError
end
end
end
instances.each do |i|
begin
PuppetCert.remove(i)
rescue PuppetError::Error
logger.error("PuppetCert.remove failed. Not fatal")
end
end
在此先感謝
編輯:爲了澄清,無論是PuppetError型是我創建的自定義類型(這裏沒有顯示),以此來識別不同類型的故障。 PuppetError :: Error是在系統調用puppet二進制失敗時發生的; PuppetError:ExecError用於Open3 :: popen3失敗時,因爲它找不到puppet二進制文件。
尚不清楚你想要做什麼。你有一個方法定義和一段調用它的代碼。對於每個部分,您想要拯救哪個錯誤類別? – sawa