LoginSignup
20
19

More than 5 years have passed since last update.

Net::HTTP のタイムアウトは Timeout::Error で拾える

Posted at

当たり前の事かも知れないのでコード読んでて、なんでってなったのでメモ書き

サンプル

require 'net/http'

begin
  http = Net::HTTP.new('http://example.com', 80)
  http.open_timeout = 5 # タイムアウト系のエラーが出るのを期待
  http.read_timeout = 10

  res = http.request(Net::HTTP::Get.new('/'))

  # たぶんレスポンスで分岐して処理をする

rescue Timeout::Error
  # timeout error
rescue => e
  # other error
end

Net::HTTP のエラー

Net::HTTP を眺めていると、それぞれのタイムアウト設定の説明には以下のように記載されています。

open_timeout
If the HTTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception.

read_timeout
If the HTTP object cannot read data in this many seconds, it raises a Net::ReadTimeout exception. The default value is 60 seconds.

なので、例外としてはそれぞれを補足するのかなぁと思っていました。

Timeout::Error のサブクラスだった

ということでした。

Class: Net::ReadTimeout

Inherits:Timeout::Error

なるほどでした。なんか timeout を使った時に出るものなのかと思ってました。

20
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
19