Skip to content

Commit

Permalink
feat(hal client): ensure meaningful error is displayed when HTTP erro…
Browse files Browse the repository at this point in the history
…rs are returned
  • Loading branch information
bethesque committed Nov 15, 2018
1 parent 4abfe7d commit 9244c14
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/pact/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions lib/pact/pact_broker/fetch_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/pact_broker/fetch_pending_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/pact/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9244c14

Please sign in to comment.