arsduo / koala

A lightweight Facebook library supporting the Graph, Marketing, and Atlas APIs, realtime updates, test users, and OAuth.

Home Page:http://developers.facebook.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraphErrorChecker#response_hash returns nil causing gem to crash

nasaabg opened this issue · comments

This is a Koala bug/issue problem.

Hi!
At @zendesk we use your gem in version 3.0.0 and I come across that issue on our Production ENV.
For some reason we have been notified about an error:

NoMethodError
undefined method `[]' for nil:NilClass

in koala/api/graph_error_checker.rb in base_error_info at line 57

Screenshot 2022-07-21 at 17 40 45

I did a little of debugging and seems that https://github.com/arsduo/koala/blob/master/lib/koala/api/graph_error_checker.rb#L60 returns nil in some cases while I assume we expect #response_hash always return Hash (empty or not).

Cases like those would return sth different than Hash and would cause gem to crash:

  • JSON.parse('true') ==> true
  • JSON.parse('null') ==> nil (our case)
  • JSON.parse('false') ==> false

I have a proposal to fix it, but I can not push my branch to the repository and open PR :(
So here it is:

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
    response_body = JSON.parse(body)
    response_body.is_a?(Hash) ? response_body : {}
  rescue JSON::ParserError
    {}
  end
end

NOTE: Looks like we are using fork but this issues is still valid

@ylecuyer any chance you may look into it? We would love to start using the original gem at @zendesk

I missed that one, I have opened #681 and will make a release in the coming days (depending on #680)

For next time, if you want to be able to create a PR on the project you can fork it, make the change on your fork, then github will propose to open a PR.

released as 3.5.0