hanklords/flickraw

JSON::ParserError

Opened this issue · 3 comments

Sometimes the flickr api returns html with status 502 and in that case I get an JSON::ParserError

JSON::ParserError: 795: unexpected token at '<!DOCTYPE html> <html lang="en-us"><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <title>Yahoo</title> <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <style> html { height: 100%; } body { background: #fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%; background-size: cover; height: 100%; text-align: center; font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, sans-serif; } table { height: 100%; width: 100%; table-layout: fixed; border-collapse: collapse; ...nt-weight: 400; color: #400090; } p { color: #1A1A1A; } #message-1 { font-weight: bold; margin: 0; } #message-2 { display: inline-block; *display: inline; zoom: 1; max-width: 17em; _width: 17em; } </style> </head> <body> <!-- status code : 502 --> <!-- Server Connection Closed rw.api.flickr.com --> <!-- host machine: r24.ycpi.bf1.yahoo.net --> <table> <tbody><tr> <td> <img src="https://s.yimg.com/nn/img/yahoo-logo-201402200629.png" alt="Yahoo Logo"> <h1 style="margin-top:20px;">Will be right back...</h1> <p id="message-1">Thank you for your patience.</p> <p id="message-2">Our engineers are working quickly to resolve the issue.</p> </td> </tr> </tbody></table> </body></html> '

Here is the stack trace

/gems/json-1.8.1/lib/json/common.rb:155 in "parse"
/gems/json-1.8.1/lib/json/common.rb:155 in "parse"
/gems/json-1.8.1/lib/json/common.rb:334 in "load"
/gems/flickraw-0.9.8/lib/flickraw/api.rb:131 in "process_response"
/gems/flickraw-0.9.8/lib/flickraw/api.rb:59 in "call"

The problem is that it checks for XML only or else it will parse it as JSON

This is an issue on flickr side... We could raise a dedicated exception on non 4XX http return code but I think it is not worth it.

I get the same thing:

JSON::ParserError: 795: unexpected token at '<html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx/1.4.4</center> </body> </html> '

It would be nice if flickraw handled the 502 and responded with the appropriate FlickRaw::FailedResponse.

This is happening more and more as Flickr starts to topple.

Even if it was just something like:

begin
  parsedJSON = JSON.parse( request.data, quirks_mode: true )
  return parsedJSON
rescue JSON::ParserError => e
  return false
end

I think we'd all be fine with that