Skip to content

Commit

Permalink
feat(webhooks): show better error message when the pb:webhooks relati…
Browse files Browse the repository at this point in the history
…on doesn't exist when trying to create a webhook
  • Loading branch information
bethesque committed Feb 21, 2022
1 parent 47bb639 commit c787300
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/client/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def url_for_relation relation_name, params
end
url
else
raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version.")
raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version. If you are using Pactflow, you may not have the permissions required for this action.")
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ def _links(key)
end

def _link!(key)
_link(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
_link(key) or raise RelationNotFoundError.new(relation_not_found_error_message(key, @href))
end

def _links!(key)
_links(key) or raise RelationNotFoundError.new("Could not find relation '#{key}' in resource at #{@href}")
_links(key) or raise RelationNotFoundError.new(relation_not_found_error_message(key, @href))
end

def embedded_entity
Expand Down Expand Up @@ -161,6 +161,10 @@ def assert_success!(_ignored = nil)
def self_href(entity_hash)
entity_hash["_links"] && entity_hash["_links"]["self"] && entity_hash["_links"]["self"]["href"]
end

def relation_not_found_error_message(key, href)
"Could not find relation '#{key}' in resource at #{href}. The most likely reason for this is that you are on an old version of the Pact Broker and you need to upgrade, or you are using Pactflow and you don't have the permissions required for this action."
end
end

class ErrorEntity < Entity
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create_webhook_with_optional_consumer_and_provider
return error_result(CREATING_WEBHOOK_WITH_UUID_NOT_SUPPORTED)
end
else
webhook_entity = index_entity._link("pb:webhooks").post(request_body_with_optional_consumer_and_provider)
webhook_entity = index_entity._link!("pb:webhooks").post(request_body_with_optional_consumer_and_provider)
end

if webhook_entity.response.status == 405
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/client/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module Hal

context 'when the key does not exist' do
it 'raises an error' do
expect { subject._link!('foo') }.to raise_error RelationNotFoundError, "Could not find relation 'foo' in resource at http://pact"
expect { subject._link!('foo') }.to raise_error RelationNotFoundError, /Could not find relation 'foo' in resource at http:\/\/pact/
end
end
end
Expand Down

0 comments on commit c787300

Please sign in to comment.