google-search-results-ruby
google-search-results-ruby copied to clipboard
JSON::ParserError raised on Cloudflare 502 Bad Gateway
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