From 8a50ebb8b71ba24b6da45031f28cb5d5be9ba963 Mon Sep 17 00:00:00 2001 From: Andrew Havens Date: Fri, 31 Mar 2017 16:32:01 -0700 Subject: [PATCH 1/3] Rescue from connection refused errors and return the appropriate status code --- lib/rack_reverse_proxy/roundtrip.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/rack_reverse_proxy/roundtrip.rb b/lib/rack_reverse_proxy/roundtrip.rb index 75cb350..a7d40e8 100644 --- a/lib/rack_reverse_proxy/roundtrip.rb +++ b/lib/rack_reverse_proxy/roundtrip.rb @@ -168,6 +168,9 @@ def rack_response_headers format_headers(target_response.headers) ) ) + rescue Errno::ECONNREFUSED => e + @_target_response = Struct.new(:status, :body).new(502, []) + {} end def replace_location_header From beaecabd9c83b4778652fe3c000b1a0554f7e8ce Mon Sep 17 00:00:00 2001 From: John Bachir Date: Wed, 7 Feb 2018 15:08:44 -0500 Subject: [PATCH 2/3] log to STDOUT when rescuing error from origin --- lib/rack_reverse_proxy/roundtrip.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/rack_reverse_proxy/roundtrip.rb b/lib/rack_reverse_proxy/roundtrip.rb index a7d40e8..df0eb4c 100644 --- a/lib/rack_reverse_proxy/roundtrip.rb +++ b/lib/rack_reverse_proxy/roundtrip.rb @@ -169,6 +169,7 @@ def rack_response_headers ) ) rescue Errno::ECONNREFUSED => e + puts "rack-reverse-proxy rescued error from origin: #{e.class.name}: #{e.message}" @_target_response = Struct.new(:status, :body).new(502, []) {} end From a25c50c2d7c95e8978b56d8b244cd8a71e28d2c0 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Wed, 7 Feb 2018 15:14:30 -0500 Subject: [PATCH 3/3] expand list of rescued origin request exceptions list and methodology taken from https://github.com/lostisland/faraday/blob/master/lib/faraday/adapter/net_http.rb --- lib/rack_reverse_proxy/roundtrip.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rack_reverse_proxy/roundtrip.rb b/lib/rack_reverse_proxy/roundtrip.rb index df0eb4c..fd48c49 100644 --- a/lib/rack_reverse_proxy/roundtrip.rb +++ b/lib/rack_reverse_proxy/roundtrip.rb @@ -162,13 +162,31 @@ def build_response_headers end end + NET_HTTP_EXCEPTIONS = [ + IOError, + Errno::ECONNABORTED, + Errno::ECONNREFUSED, + Errno::ECONNRESET, + Errno::EHOSTUNREACH, + Errno::EINVAL, + Errno::ENETUNREACH, + Errno::EPIPE, + Net::HTTPBadResponse, + Net::HTTPHeaderSyntaxError, + Net::ProtocolError, + SocketError, + Zlib::GzipFile::Error, + ] + NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL) + NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout) + def rack_response_headers Rack::Utils::HeaderHash.new( Rack::Proxy.normalize_headers( format_headers(target_response.headers) ) ) - rescue Errno::ECONNREFUSED => e + rescue *NET_HTTP_EXCEPTIONS => e puts "rack-reverse-proxy rescued error from origin: #{e.class.name}: #{e.message}" @_target_response = Struct.new(:status, :body).new(502, []) {}