Skip to content

Commit

Permalink
Merge pull request #60 from gmaliar/fix/500-errors-json-parse
Browse files Browse the repository at this point in the history
Handle 500 errors as Unsplash::Error
  • Loading branch information
aaronklaassen authored Mar 19, 2019
2 parents 65404ea + e7f5181 commit 05c36fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/unsplash/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ def request(verb, path, params = {})

status_code = response.respond_to?(:status) ? response.status : response.code

if !(200..299).include?(status_code)
body = JSON.parse(response.body)
msg = body["error"] || body["errors"].join(" ")
raise Unsplash::Error.new msg
begin
if !(200..299).include?(status_code)
body = JSON.parse(response.body)
msg = body["error"] || body["errors"].join(" ")
raise Unsplash::Error.new msg
end
rescue JSON::ParserError
raise Unsplash::Error.new response.body
end

response
Expand Down
11 changes: 11 additions & 0 deletions spec/unsplash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@
expect(Unsplash.configuration.logger).to receive(:warn).with("Watch out!")
Unsplash::CuratedBatch.all
end

it "handles 5** errors as Unsplash Errors" do
response = double(
body: "We are experiencing errors. Please check https://status.unsplash.com for updates.",
status: 503,
headers: { "Content-Type" => "text/plain" }
)
allow(Unsplash::Connection).to receive(:public_send).and_return(response)

expect { Unsplash::CuratedBatch.all }.to raise_error(Unsplash::Error)
end
end

0 comments on commit 05c36fa

Please sign in to comment.