diff --git a/Changelog.md b/Changelog.md index 787774e78..5b8a54165 100644 --- a/Changelog.md +++ b/Changelog.md @@ -58,6 +58,7 @@ We use yarn to install the frontend dependencies now, so you need to have that i * Update the suggested Ruby version to 2.7. If you run into trouble during the update and you followed our installation guides, run `rvm install 2.7`. [#8366](https://github.com/diaspora/diaspora/pull/8366) * Upgrade to bundler 2 [#8366](https://github.com/diaspora/diaspora/pull/8366) * Stop checking `/.well-known/host-meta`, check for `/.well-known/nodeinfo` instead [#8377](https://github.com/diaspora/diaspora/pull/8377) +* Handle NodeInfo timeouts gracefully [#8380](https://github.com/diaspora/diaspora/pull/8380) ## Bug fixes * Fix that no mails were sent after photo export [#8365](https://github.com/diaspora/diaspora/pull/8365) diff --git a/app/models/pod.rb b/app/models/pod.rb index c64bf4ce2..08692f280 100644 --- a/app/models/pod.rb +++ b/app/models/pod.rb @@ -114,7 +114,7 @@ class Pod < ApplicationRecord def update_from_result(result) self.status = status_from_result(result) update_offline_since - logger.warn "OFFLINE #{result.failure_message}" if offline? + logger.warn "#{uri} OFFLINE: #{result.failure_message}" if offline? attributes_from_result(result) touch(:checked_at) @@ -125,7 +125,7 @@ class Pod < ApplicationRecord def attributes_from_result(result) self.ssl ||= result.ssl - self.error = result.failure_message[0..254] if result.error? + self.error = result.error? ? result.failure_message[0..254] : nil self.software = result.software_version[0..254] if result.software_version.present? self.response_time = result.rt end diff --git a/lib/connection_tester.rb b/lib/connection_tester.rb index 4fd210b93..f278cc8c1 100644 --- a/lib/connection_tester.rb +++ b/lib/connection_tester.rb @@ -109,7 +109,7 @@ class ConnectionTester raise NetFailure, e.message rescue Faraday::SSLError => e raise SSLFailure, e.message - rescue ArgumentError, Faraday::ClientError => e + rescue ArgumentError, Faraday::ClientError, Faraday::ServerError => e raise HTTPFailure, "#{e.class}: #{e.message}" rescue StandardError => e unexpected_error(e) @@ -133,7 +133,7 @@ class ConnectionTester raise HTTPFailure, "#{e.class}: #{e.message}" rescue NodeInfoFailure => e raise e - rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError => e + rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError, Faraday::TimeoutError => e raise NodeInfoFailure, "#{e.class}: #{e.message}" rescue JSON::JSONError => e raise NodeInfoFailure, e.message[0..255].encode(Encoding.default_external, undef: :replace) diff --git a/spec/lib/connection_tester_spec.rb b/spec/lib/connection_tester_spec.rb index 671fec860..d665dd717 100644 --- a/spec/lib/connection_tester_spec.rb +++ b/spec/lib/connection_tester_spec.rb @@ -94,6 +94,11 @@ describe ConnectionTester do expect { tester.request }.to raise_error(ConnectionTester::HTTPFailure) end + it "receives a 502 bad gateway" do + stub_request(:get, url).to_return(status: 502, body: "Bad Gateway!") + expect { tester.request }.to raise_error(ConnectionTester::HTTPFailure) + end + it "cannot connect" do stub_request(:get, url).to_raise(Faraday::ConnectionFailed.new("Error!")) expect { tester.request }.to raise_error(ConnectionTester::NetFailure) @@ -172,6 +177,14 @@ describe ConnectionTester do expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure) end + it "handles timeout gracefully" do + ni_wellknown = {links: [{rel: "http://nodeinfo.diaspora.software/ns/schema/1.0", href: "/nodeinfo/1.0"}]} + stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}") + .to_return(status: 200, body: JSON.generate(ni_wellknown)) + stub_request(:get, "#{url}/nodeinfo/1.0").to_raise(Faraday::TimeoutError.new) + expect { tester.nodeinfo }.to raise_error(ConnectionTester::NodeInfoFailure) + end + it "handles a invalid jrd document gracefully" do invalid_wellknown = {links: {rel: "http://nodeinfo.diaspora.software/ns/schema/1.0", href: "/nodeinfo/1.0"}} stub_request(:get, "#{url}#{ConnectionTester::NODEINFO_FRAGMENT}") diff --git a/spec/models/pod_spec.rb b/spec/models/pod_spec.rb index b7386f7a6..ac5520c73 100644 --- a/spec/models/pod_spec.rb +++ b/spec/models/pod_spec.rb @@ -143,31 +143,28 @@ describe Pod, type: :model do describe "#test_connection!" do before do @pod = FactoryBot.create(:pod) - @result = double("result") @now = Time.zone.now - allow(@result).to receive(:rt) { 123 } - allow(@result).to receive(:software_version) { "diaspora a.b.c.d" } - allow(@result).to receive(:failure_message) { "hello error!" } + @result = ConnectionTester::Result.new + @result.rt = 123 + @result.software_version = "diaspora a.b.c.d" + @result.error = ConnectionTester::NetFailure.new("hello error!") expect(ConnectionTester).to receive(:check).at_least(:once).and_return(@result) end it "updates the connectivity values" do - allow(@result).to receive(:error) - allow(@result).to receive(:error?) + @result.error = nil @pod.test_connection! expect(@pod.status).to eq("no_errors") - expect(@pod.offline?).to be_falsy + expect(@pod.offline?).to be_falsey expect(@pod.response_time).to eq(123) expect(@pod.checked_at).to be_within(1.second).of @now end it "resets the scheduled_check flag" do - allow(@result).to receive(:error) - allow(@result).to receive(:error?) - @pod.update_column(:scheduled_check, true) + @pod.update(scheduled_check: true) @pod.test_connection! @@ -175,17 +172,22 @@ describe Pod, type: :model do end it "handles a failed check" do - expect(@result).to receive(:error?).at_least(:once) { true } - expect(@result).to receive(:error).at_least(:once) { ConnectionTester::NetFailure.new } @pod.test_connection! expect(@pod.offline?).to be_truthy expect(@pod.offline_since).to be_within(1.second).of @now end + it "removes the error message when there was no error" do + @pod.update(error: "old error message") + + @result.error = nil + @pod.test_connection! + + expect(@pod.error).to be_nil + end + it "preserves the original offline timestamp" do - expect(@result).to receive(:error?).at_least(:once) { true } - expect(@result).to receive(:error).at_least(:once) { ConnectionTester::NetFailure.new } @pod.test_connection! expect(@pod.offline_since).to be_within(1.second).of @now