Skip to content

Commit

Permalink
Fix: Return ClientError instead of NoMethodError when body is 'null' (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ylecuyer committed Aug 23, 2023
1 parent ad26a84 commit a74f498
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Updated features:
Removed features:

Internal improvements:
* Raise ClientError instead of NoMethodError when body is 'null' ([#673](https://github.com/arsduo/koala/issues/673))

Testing improvements:

Expand Down
3 changes: 2 additions & 1 deletion lib/koala/api/graph_error_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def response_hash
# Normally, we start with the response body. If it isn't valid JSON, we start with an empty
# hash and fill it with error data.
@response_hash ||= begin
JSON.parse(body)
parsed_body = JSON.parse(body)
parsed_body.is_a?(Hash) ? parsed_body : {}
rescue JSON::ParserError
{}
end
Expand Down
6 changes: 6 additions & 0 deletions spec/cases/graph_error_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ module Facebook
expect(error.response_body).to eq(body)
end

it "returns a ClientError if the body is null" do
body.replace("null")
expect(error).to be_a(ClientError)
expect(error.response_body).to eq(body)
end

it "adds error data from the body" do
error_data = {
"type" => "FB error type",
Expand Down

0 comments on commit a74f498

Please sign in to comment.