From 300f8357030ba830948f7e6aa4fa03e3fe9d9780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Thu, 9 Jun 2022 16:30:22 -0300 Subject: [PATCH] DEV: Supress logs when RetrieveTitle.crawl fails with Net::ReadTimeout errors (#16971) This PR changes the rescue block to rescue only Net::TimeoutError exceptions and removes the log line to prevent clutter the logs with errors that are ignored. Other errors can bubble up because they're errors we probably want to know about --- lib/retrieve_title.rb | 6 ++---- spec/lib/retrieve_title_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/retrieve_title.rb b/lib/retrieve_title.rb index c71d69e2295..2a8ff118e3f 100644 --- a/lib/retrieve_title.rb +++ b/lib/retrieve_title.rb @@ -9,10 +9,8 @@ module RetrieveTitle max_redirects: max_redirects, initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit ) - rescue Exception => ex - raise if Rails.env.test? - Rails.logger.error(ex) - nil + rescue Net::ReadTimeout + # do nothing for Net::ReadTimeout errors end def self.extract_title(html, encoding = nil) diff --git a/spec/lib/retrieve_title_spec.rb b/spec/lib/retrieve_title_spec.rb index 91852ca6c90..6711f233f16 100644 --- a/spec/lib/retrieve_title_spec.rb +++ b/spec/lib/retrieve_title_spec.rb @@ -142,6 +142,18 @@ describe RetrieveTitle do expect(RetrieveTitle.crawl("https://example.com")).to eq(nil) end + + it "it raises errors other than Net::ReadTimeout, e.g. NoMethodError" do + stub_request(:get, "https://example.com").to_raise(NoMethodError) + + expect { RetrieveTitle.crawl("https://example.com") }.to raise_error(NoMethodError) + end + + it "it ignores Net::ReadTimeout errors" do + stub_request(:get, "https://example.com").to_raise(Net::ReadTimeout) + + expect { RetrieveTitle.crawl("https://example.com") }.not_to raise_error(Net::ReadTimeout) + end end context 'fetch_title' do