From a8ba1ed7ece688d2eeb08427d525fd8de84da081 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 16 Jul 2018 07:44:49 +1000 Subject: [PATCH] fix: show a more helpful error when attempting to parse a URI that is not a pact Closes: https://github.com/pact-foundation/pact-provider-verifier/issues/25 --- .../consumer_contract/consumer_contract.rb | 6 +++++- lib/pact/consumer_contract/pact_file.rb | 4 ++-- spec/fixtures/not-a-pact.json | 3 +++ .../consumer_contract_spec.rb | 20 +++++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/not-a-pact.json diff --git a/lib/pact/consumer_contract/consumer_contract.rb b/lib/pact/consumer_contract/consumer_contract.rb index 3c1bb4a..75c8c82 100644 --- a/lib/pact/consumer_contract/consumer_contract.rb +++ b/lib/pact/consumer_contract/consumer_contract.rb @@ -14,6 +14,8 @@ module Pact + class UnrecognizePactFormatError < ::Pact::Error; end + class ConsumerContract include SymbolizeKeys @@ -42,7 +44,7 @@ def self.from_hash(hash) parsers.each do | parser | return parser.call(hash) if parser.can_parse?(hash) end - raise Pact::Error.new("No consumer contract parser found for hash: #{hash}") + raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact format: #{hash}") end def self.from_json string @@ -52,6 +54,8 @@ def self.from_json string def self.from_uri uri, options = {} from_json(Pact::PactFile.read(uri, options)) + rescue UnrecognizePactFormatError + raise Pact::UnrecognizePactFormatError.new("This document does not use a recognised Pact format. Please check that #{uri} is a valid pact file.") end def self.maintain_backwards_compatiblity_with_producer_keys string diff --git a/lib/pact/consumer_contract/pact_file.rb b/lib/pact/consumer_contract/pact_file.rb index 81e30ca..b373839 100644 --- a/lib/pact/consumer_contract/pact_file.rb +++ b/lib/pact/consumer_contract/pact_file.rb @@ -40,7 +40,7 @@ def render_pact(uri_string, options) end private - + def local? uri !uri.start_with?("http://", "https://") end @@ -74,7 +74,7 @@ def get_remote_with_retry(uri_string, options) end end end - + def get_remote(uri, options) request = Net::HTTP::Get.new(uri) request.basic_auth(options[:username], options[:password]) if options[:username] diff --git a/spec/fixtures/not-a-pact.json b/spec/fixtures/not-a-pact.json new file mode 100644 index 0000000..c8c4105 --- /dev/null +++ b/spec/fixtures/not-a-pact.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/spec/lib/pact/consumer_contract/consumer_contract_spec.rb b/spec/lib/pact/consumer_contract/consumer_contract_spec.rb index 18c1609..7c7649e 100644 --- a/spec/lib/pact/consumer_contract/consumer_contract_spec.rb +++ b/spec/lib/pact/consumer_contract/consumer_contract_spec.rb @@ -3,6 +3,26 @@ module Pact describe ConsumerContract do + describe "from_uri" do + context "when the URL does not point to a valid pact" do + subject { ConsumerContract.from_uri('spec/fixtures/not-a-pact.json') } + + it "raises a helpful error" do + expect { subject }.to raise_error UnrecognizePactFormatError, /Please check that spec/ + end + end + end + + describe "from_hash" do + context "when the hash is not a valid pact" do + subject { ConsumerContract.from_hash({'foo' => 'bar'}) } + + it "raises a helpful error" do + expect { subject }.to raise_error UnrecognizePactFormatError, 'This document does not use a recognised Pact format: {"foo"=>"bar"}' + end + end + end + describe ".from_json" do let(:loaded_pact) { ConsumerContract.from_json(string) }