Skip to content

A platform for community discussion. Free, open, simple.

FIX: Don’t raise an error on onebox timeouts

Loïc Guitaut1f5682b7d7ba
diff --git a/lib/final_destination.rb b/lib/final_destination.rb
index 6426924232b..7070bcfdeec 100644
--- a/lib/final_destination.rb
+++ b/lib/final_destination.rb
@@ -518,6 +518,9 @@ class FinalDestination
end
result
+ rescue Timeout::Error
+ log(:warn, "FinalDestination could not resolve URL (timeout): #{@uri}") if @verbose
+ nil
rescue StandardError
unsafe_close ? [:ok, headers_subset] : raise
end
diff --git a/spec/lib/final_destination_spec.rb b/spec/lib/final_destination_spec.rb
index 2c78f440ce8..3d0e1749e27 100644
--- a/spec/lib/final_destination_spec.rb
+++ b/spec/lib/final_destination_spec.rb
@@ -424,28 +424,46 @@ describe FinalDestination do
end
end
- describe '.get' do
+ describe '#get' do
+ let(:fd) { FinalDestination.new("http://wikipedia.com", opts.merge(verbose: true)) }
- it "can correctly stream with a redirect" do
- FinalDestination.clear_https_cache!("wikipedia.com")
+ context "when there is a redirect" do
+ before do
+ described_class.clear_https_cache!("wikipedia.com")
+ stub_request(:get, "http://wikipedia.com/").
+ to_return(status: 302, body: "" , headers: { "location" => "https://wikipedia.com/" })
+ # webmock does not do chunks
+ stub_request(:get, "https://wikipedia.com/").
+ to_return(status: 200, body: "<html><head>" , headers: {})
+ end
+
+ it "correctly streams" do
+ chunk = nil
+ result = fd.get do |resp, c|
+ chunk = c
+ throw :done
+ end
- stub_request(:get, "http://wikipedia.com/").
- to_return(status: 302, body: "" , headers: { "location" => "https://wikipedia.com/" })
+ expect(result).to eq("https://wikipedia.com/")
+ expect(chunk).to eq("<html><head>")
+ end
+ end
- # webmock does not do chunks
- stub_request(:get, "https://wikipedia.com/").
- to_return(status: 200, body: "<html><head>" , headers: {})
+ context "when there is a timeout" do
+ subject(:get) { fd.get {} }
- result = nil
- chunk = nil
+ before do
+ fd.stubs(:safe_session).raises(Timeout::Error)
+ end
- result = FinalDestination.new("http://wikipedia.com", opts).get do |resp, c|
- chunk = c
- throw :done
+ it "logs the exception" do
+ Rails.logger.expects(:warn).with("default: FinalDestination could not resolve URL (timeout): https://wikipedia.com")
+ get
end
- expect(result).to eq("https://wikipedia.com/")
- expect(chunk).to eq("<html><head>")
+ it "returns nothing" do
+ expect(get).to be_blank
+ end
end
end