Timeout::Errorのrescueについて記述する。
現象
以下のようなコードを実行して、サーバー側で処理に時間がかかった場合にはTimeout::Errorが発生する。
しかし、rescueでキャッチされない。
-
|
サンプルコード |
require 'net/http'
begin
Net::HTTP.start("hogehoge", 80) do |http|
...
end
rescue
puts "ERROR"
end
|
原因
-
|
サンプルコード |
require 'timeout'
begin
timeout(1) do
sleep(10)
end
rescue
puts "ERROR"
end
|
対策
- Timeout::Errorもしくは、Interruptを捕捉する。
rescue Timeout::Error, StandardError => e
rescue Interrupt => e
...
resceu => e
参照リンク
最終更新:2010年11月03日 12:27