From 1d1ff2e0474997f7e0f3fab3b0fe00bb55d93593 Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Mon, 4 May 2020 09:25:41 +0200 Subject: [PATCH] NetHttpPersistent: ConnectionError on OpenTimeout - open timeout is treated as a failure to connect - 4.0.0 and up supports this --- Gemfile | 2 +- lib/faraday/adapter/net_http_persistent.rb | 2 ++ spec/support/shared_examples/request_method.rb | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 68d087d14..1a767f8cb 100644 --- a/Gemfile +++ b/Gemfile @@ -23,7 +23,7 @@ group :test, :development do gem 'excon', '>= 0.27.4' gem 'httpclient', '>= 2.2' gem 'multipart-parser' - gem 'net-http-persistent' + gem 'net-http-persistent', '~> 4.0' gem 'patron', '>= 0.4.2', platforms: :ruby gem 'rack', '< 2.1' gem 'rack-test', '>= 0.6', require: 'rack/test' diff --git a/lib/faraday/adapter/net_http_persistent.rb b/lib/faraday/adapter/net_http_persistent.rb index ff20c2567..22639a22f 100644 --- a/lib/faraday/adapter/net_http_persistent.rb +++ b/lib/faraday/adapter/net_http_persistent.rb @@ -53,6 +53,8 @@ def perform_request(http, env) http.request env[:url], create_request(env) rescue Errno::ETIMEDOUT => e raise Faraday::TimeoutError, e + rescue Net::OpenTimeout => e + raise Faraday::ConnectionFailed, e rescue Net::HTTP::Persistent::Error => e raise Faraday::TimeoutError, e if e.message.include? 'Timeout' diff --git a/spec/support/shared_examples/request_method.rb b/spec/support/shared_examples/request_method.rb index 8e2828a21..be1bfc25e 100644 --- a/spec/support/shared_examples/request_method.rb +++ b/spec/support/shared_examples/request_method.rb @@ -99,7 +99,7 @@ it 'supports timeout option' do conn_options[:request] = { timeout: 1 } request_stub.to_timeout - exc = adapter == 'NetHttp' ? Faraday::ConnectionFailed : Faraday::TimeoutError + exc = %w[NetHttp NetHttpPersistent].include?(adapter) ? Faraday::ConnectionFailed : Faraday::TimeoutError expect { conn.public_send(http_method, '/') }.to raise_error(exc) end @@ -108,7 +108,7 @@ it 'supports open_timeout option' do conn_options[:request] = { open_timeout: 1 } request_stub.to_timeout - exc = adapter == 'NetHttp' ? Faraday::ConnectionFailed : Faraday::TimeoutError + exc = %w[NetHttp NetHttpPersistent].include?(adapter) ? Faraday::ConnectionFailed : Faraday::TimeoutError expect { conn.public_send(http_method, '/') }.to raise_error(exc) end