diff --git a/lib/rgrb/plugin/url_fetch_title/generator.rb b/lib/rgrb/plugin/url_fetch_title/generator.rb index d419439b..261f8646 100644 --- a/lib/rgrb/plugin/url_fetch_title/generator.rb +++ b/lib/rgrb/plugin/url_fetch_title/generator.rb @@ -2,6 +2,7 @@ require 'uri' require 'socket' +require 'net/http' require 'mechanize' require 'active_support' require 'active_support/core_ext/numeric/conversions' @@ -117,6 +118,8 @@ def fetch_title(url) end rescue SocketError body = '(サーバーに接続できませんでした)' + rescue Net::HTTP::Persistent::Error + body = '(タイムアウト)' rescue Mechanize::ResponseCodeError => response_code_error response_code = response_code_error.response_code.to_i reason_phrase = diff --git a/spec/rgrb/plugin/url_fetch_title/generator_spec.rb b/spec/rgrb/plugin/url_fetch_title/generator_spec.rb index 1e809acf..d35c09e1 100644 --- a/spec/rgrb/plugin/url_fetch_title/generator_spec.rb +++ b/spec/rgrb/plugin/url_fetch_title/generator_spec.rb @@ -111,6 +111,21 @@ end end + context 'タイムアウトした場合' do + let(:url) { 'http://example.net/very_very_heavy' } + + before do + stub_request(:get, url).to_timeout + end + + subject { generator.fetch_title(url) } + it 'タイムアウトしたことを示すエラーメッセージを返す' do + expect(subject).to eq( + "#{default_prefix}(タイムアウト)#{default_suffix}" + ) + end + end + context '401 Unauthorized' do let(:url) { 'http://example.net/need_login' }