Skip to content

Commit

Permalink
fix: show a more helpful error when attempting to parse a URI that is…
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 15, 2018
1 parent f5b6e6b commit a8ba1ed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/pact/consumer_contract/consumer_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

module Pact

class UnrecognizePactFormatError < ::Pact::Error; end

class ConsumerContract

include SymbolizeKeys
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/pact/consumer_contract/pact_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def render_pact(uri_string, options)
end

private

def local? uri
!uri.start_with?("http://", "https://")
end
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/not-a-pact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
20 changes: 20 additions & 0 deletions spec/lib/pact/consumer_contract/consumer_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit a8ba1ed

Please sign in to comment.