A platform for community discussion. Free, open, simple.
FIX: Make error in Discourse Hub more descriptive. (#6438)
Bianca Nenciu1fb1f4c79088
diff --git a/lib/discourse_hub.rb b/lib/discourse_hub.rbindex bedcaeb7ce0..38e5c101a17 100644--- a/lib/discourse_hub.rb+++ b/lib/discourse_hub.rb@@ -52,14 +52,25 @@ module DiscourseHubdef self.collection_action(action, rel_url, params = {})connect_opts = connect_opts(params)- JSON.parse(Excon.send(action,++ response = Excon.send(action,"#{hub_base_url}#{rel_url}",{body: JSON[params],headers: { 'Referer' => referer, 'Accept' => accepts.join(', '), "Content-Type" => "application/json" },omit_default_port: true}.merge(connect_opts)- ).body)+ )++ if response.status != 200+ Rails.logger.warn("Discourse Hub (#{hub_base_url}#{rel_url}) returned a bad status #{response.status}.")+ end++ begin+ JSON.parse(response.body)+ rescue JSON::ParserError+ Rails.logger.error("Discourse Hub returned a bad response body: " + response.body)+ endenddef self.connect_opts(params = {})diff --git a/spec/components/discourse_hub_spec.rb b/spec/components/discourse_hub_spec.rbindex c97e7940e65..a42255b508c 100644--- a/spec/components/discourse_hub_spec.rb+++ b/spec/components/discourse_hub_spec.rb@@ -75,4 +75,25 @@ describe DiscourseHub doendendend++ describe '.collection_action' do++ it 'should log a warning if status is not 200' do+ stub_request(:get, (ENV['HUB_BASE_URL'] || "http://local.hub:3000/api")).+ to_return(status: 500, body: "", headers: {})++ Rails.logger.expects(:warn)++ DiscourseHub.collection_action(:get, "")+ end++ it 'should log an error if response is invalid JSON' do+ stub_request(:get, (ENV['HUB_BASE_URL'] || "http://local.hub:3000/api")).+ to_return(status: 200, body: "this is not valid JSON", headers: {})++ Rails.logger.expects(:error)++ DiscourseHub.collection_action(:get, "")+ end+ endend