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

JSON::ParserError raised on Cloudflare 502 Bad Gateway #16

Open
jeremysmithco opened this issue Aug 28, 2023 · 0 comments
Open

JSON::ParserError raised on Cloudflare 502 Bad Gateway #16

jeremysmithco opened this issue Aug 28, 2023 · 0 comments

Comments

@jeremysmithco
Copy link

Hey there!

This isn't a super critical issue, but today we hit a few exceptions like the following when calling SerpApi with the gem and getting a 502 repsonse from Cloudflare:

JSON::ParserError: 767: unexpected token at '<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
'

Here's the relevant part of the backtrace:

/app/vendor/ruby-2.6.10/lib/ruby/2.6.0/json/common.rb:156:in `parse'
/app/vendor/ruby-2.6.10/lib/ruby/2.6.0/json/common.rb:335:in `load'
/app/vendor/bundle/ruby/2.6.0/gems/google_search_results-2.2.0/lib/search/serp_api_search.rb:141:in `rescue in get_results'
/app/vendor/bundle/ruby/2.6.0/gems/google_search_results-2.2.0/lib/search/serp_api_search.rb:136:in `get_results'
/app/vendor/bundle/ruby/2.6.0/gems/google_search_results-2.2.0/lib/search/serp_api_search.rb:57:in `get_html'

The Cloudflare response is HTML, but the gem assumes the response will be JSON and tries to parse it:

if error = JSON.load(e.io.read)["error"]
  puts "server returns error for url: #{url}"
  raise error
else
  puts "fail: fetch url: #{url}"
  raise e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant