Skip to content

Handle 502 errors #10

@JustinAiken

Description

@JustinAiken

The error:

I've noticed that occasionally the Readability API has a 502 error instead of a json response.:

<html>
  <head>
    <title>502 Bad Gateway</title>
  </head> 
  <body bgcolor="white">
    <center>
      <h1>502 Bad Gateway</h1>
    </center>
    <hr>
    <center>nginx/1.4.6 (Ubuntu)</center> 
  </body> 
</html>

The response.body is then an actual HTML body instead of a JSON string, so this code:

response = http.request(request)
Hashie::Mash.new MultiJson.decode(response.body)

passes an unparsable string to MultiJson.decode, resulting in this error:

MultiJson::ParseError: 795: unexpected token at
 '<html> <head><title>502 Bad Gateway</title></head>... '
/usr/local/rbenv/versions/2.1.1/lib/ruby/2.1.0/json/common.rb:155→ parse
/usr/local/rbenv/versions/2.1.1/lib/ruby/2.1.0/json/common.rb:155→ parse
multi_json-1.10.1/lib/multi_json/adapters/json_common.rb:16→ load
multi_json-1.10.1/lib/multi_json/adapter.rb:20→ load
multi_json-1.10.1/lib/multi_json.rb:119→ load
readit-0.1.0/lib/readit.rb:78→ https_request
readit-0.1.0/lib/readit.rb:53→ parse

Possible error catching:

  • Rescue MultiJson::ParseError
  • Check response status before passing to JSON parser

Possible error handling:

  • raise ReaditError
  • return empty hash

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions