From a74f49805cf554e1a544c7aceb57fd67d13fd3de Mon Sep 17 00:00:00 2001 From: Yoann Lecuyer Date: Wed, 23 Aug 2023 22:55:45 +0200 Subject: [PATCH] Fix: Return ClientError instead of NoMethodError when body is 'null' (#681) --- changelog.md | 1 + lib/koala/api/graph_error_checker.rb | 3 ++- spec/cases/graph_error_checker_spec.rb | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 16b6cac8..c6822fd8 100644 --- a/changelog.md +++ b/changelog.md @@ -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: diff --git a/lib/koala/api/graph_error_checker.rb b/lib/koala/api/graph_error_checker.rb index e3ce536a..9fb07823 100644 --- a/lib/koala/api/graph_error_checker.rb +++ b/lib/koala/api/graph_error_checker.rb @@ -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 diff --git a/spec/cases/graph_error_checker_spec.rb b/spec/cases/graph_error_checker_spec.rb index 877fabac..6245d433 100644 --- a/spec/cases/graph_error_checker_spec.rb +++ b/spec/cases/graph_error_checker_spec.rb @@ -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",