Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue invalid JSON parsing #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/contentful_lite/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def request(endpoint, parameters)
JSON.parse(body).tap do |parsed|
raise error_class(response.status).new(response, parsed) if response.status != 200
end
rescue JSON::ParserError
raise error_class(response.status).new(response, body)
end

def request_headers
Expand Down
14 changes: 14 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
it { is_expected.to be_a(ContentfulLite::EntriesArray) }
it { expect(subject.size).to eq 2 }
end

context 'response JSON is invalid' do
before do
stub_request(:get, %r{contentful.com/spaces/.*/entries}).
to_return(body: "connection failure")
end

it do
expect { subject }.to raise_error(
ContentfulLite::Client::RequestError,
"Invalid Contentful Response: connection failure"
)
end
end
end

describe '#entry' do
Expand Down
Loading