From 9244c14642c90e25ed9da35559479137f4c5d0ec Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 15 Nov 2018 15:02:19 +1100 Subject: [PATCH] feat(hal client): ensure meaningful error is displayed when HTTP errors are returned --- lib/pact/hal/entity.rb | 10 ++++++++ lib/pact/pact_broker/fetch_pacts.rb | 6 ++--- lib/pact/pact_broker/fetch_pending_pacts.rb | 2 +- spec/lib/pact/hal/entity_spec.rb | 26 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/pact/hal/entity.rb b/lib/pact/hal/entity.rb index 37755619..52523f04 100644 --- a/lib/pact/hal/entity.rb +++ b/lib/pact/hal/entity.rb @@ -7,6 +7,8 @@ module Pact module Hal class RelationNotFoundError < ::Pact::Error; end + class ErrorResponseReturned < ::Pact::Error; end + class Entity def initialize(href, data, http_client, response = nil) @@ -76,6 +78,10 @@ def method_missing(method_name, *args, &block) def respond_to_missing?(method_name, include_private = false) @data.key?(method_name) || @links.key?(method_name) end + + def assert_success! + self + end end class ErrorEntity < Entity @@ -91,6 +97,10 @@ def initialize(href, data, http_client, response = nil) def success? false end + + def assert_success! + raise ErrorResponseReturned.new("Error retrieving #{@href} status=#{response ? response.code: nil} #{response ? response.raw_body : ''}") + end end end end diff --git a/lib/pact/pact_broker/fetch_pacts.rb b/lib/pact/pact_broker/fetch_pacts.rb index 64655519..f460dc81 100644 --- a/lib/pact/pact_broker/fetch_pacts.rb +++ b/lib/pact/pact_broker/fetch_pacts.rb @@ -72,16 +72,16 @@ def link_for(tag) end def index - @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get + @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get.assert_success! end def latest_pacts_for_provider link = index_entity._link!(LATEST_PROVIDER_RELATION) - pact_urls(link.expand(provider: provider).get) + pact_urls(link.expand(provider: provider).get.assert_success!) end def pact_urls(link_by_provider) - link_by_provider.fetch(PB_PACTS, PACTS).collect do |pact| + link_by_provider.assert_success!.fetch(PB_PACTS, PACTS).collect do |pact| Pact::Provider::PactURI.new(pact[HREF], http_client_options) end end diff --git a/lib/pact/pact_broker/fetch_pending_pacts.rb b/lib/pact/pact_broker/fetch_pending_pacts.rb index 579ef681..10faee5d 100644 --- a/lib/pact/pact_broker/fetch_pending_pacts.rb +++ b/lib/pact/pact_broker/fetch_pending_pacts.rb @@ -36,7 +36,7 @@ def call private def index - @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get + @index_entity ||= Pact::Hal::Link.new({ "href" => broker_base_url }, http_client).get.assert_success! end def pending_pacts_for_provider diff --git a/spec/lib/pact/hal/entity_spec.rb b/spec/lib/pact/hal/entity_spec.rb index e444cbba..4eb4744d 100644 --- a/spec/lib/pact/hal/entity_spec.rb +++ b/spec/lib/pact/hal/entity_spec.rb @@ -60,6 +60,32 @@ module Hal end end + describe "assert_success!" do + context "when the response is successful" do + it "returns the entity" do + expect(entity.assert_success!).to be entity + end + end + + context "when the response is not successful and there is no response" do + subject(:entity) { ErrorEntity.new("http://pact", pact_hash, http_client) } + + it "raises an error" do + expect { entity.assert_success! }.to raise_error Pact::Hal::ErrorResponseReturned, "Error retrieving http://pact status= " + end + end + + context "when the response is not successful and there is a response" do + let(:response) { double('response', code: 200, raw_body: "body") } + + subject(:entity) { ErrorEntity.new("http://pact", pact_hash, http_client, response) } + + it "raises an error" do + expect { entity.assert_success! }.to raise_error Pact::Hal::ErrorResponseReturned, "Error retrieving http://pact status=200 body" + end + end + end + describe "can?" do context "when the relation exists" do it "returns true" do