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

Events API raises JSON::ParserError when server responds with HTML error #51

Open
nmdoliveira opened this issue Jan 21, 2020 · 4 comments
Labels

Comments

@nmdoliveira
Copy link

Hi! A few days ago, I observed the same error as described in #47, but with the Events API. I believe there was some instability on your end and my app received some HTML errors, and this gem raised JSON::ParserError.

Expected Behavior

When RD Events API responds with an error, even if in HTML format, the gem should raise a semantic error, for instance RDStation::Error::BadGateway.

Current Behavior

JSON::ParserError is raised instead.

Failure Information

Gem version

2.1.0

Sample trace

JSON::ParserError: 785: unexpected token at '<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Erro | RD </title>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700&display=swap" rel="stylesheet">
    <style type="text/css" rel="stylesheet">
      body { background-color: #E8EBF1; color: #364A65; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; margin: 0; }
      h1, h2 { margin-bottom: 10px; }
      a { color: #33A3FF; font-weight: bold; }
      .navbar { background-color: #364A65; color: #FFF; font-size: 22px; padding: 12px 0; text-align: center; position: fixed; width: 100%; top: 0; left: 0; }
      .row { display: -webkit-flex; display: -ms-flex; display: flex; align-items: center; justify-content: center; min-height: 100vh; }
      .wrapper { width: 500px; text-align: center; }
      .wrapper h1 { font-size: 100px; font-weight: bold; }
      .wrapper h2 { font-size: 18px; font-weight: normal; }
      .wrapper .copyright-info { margin-top: 40px; font-size: 14px; }
      @media (max-width: 500px) {
        .wrapper { width: auto; }
        .wrapper h1 { font-size: 60px; }
      }
    </style>
  </head>
  <body>
    <div class="navbar">
      <div class="logo"><strong>RD</strong></div>
    </div>
    <div class="row">
      <div class="wrapper">
        <h1>Oops...</h1>
        
          <h2>Parece que estamos com uma instabilidade, aguarde uns minutos e tente atualizar a página.</h2>
          <h2> Serviço <i>galaga</i> retornou código de erro <i>502</i>.</h2>
        
        <div class="copyright-info">© 2011-2019 | Todos os direitos reservados</div>
      </div>
    </div>
  </body>
</html>
'
  from json/common.rb:156:in `parse'
  from json/common.rb:156:in `parse'
  from rdstation/events.rb:13:in `create'

Solution

I can't be completely sure of the status code of the response, but the HTML body mentioned 502, so I'm assuming the status code was the same. I then checked the source of the Events API and it is not using the ApiResponse module, which is the one that was changed to check status codes before parsing as a fix for #47, so I think this is the cause:

def create(payload)
retryable_request(@authorization) do |authorization|
response = self.class.post(EVENTS_ENDPOINT, headers: authorization.headers, body: payload.to_json)
response_body = JSON.parse(response.body)
return response_body unless errors?(response_body)
RDStation::ErrorHandler.new(response).raise_error
end
end

A possible fix is to change that to use ApiResponse like the other APIs, for instance Contacts:

def update(uuid, contact_hash)
retryable_request(@authorization) do |authorization|
response = self.class.patch(base_url(uuid), :body => contact_hash.to_json, :headers => authorization.headers)
ApiResponse.build(response)
end
end

If that sounds good, I can open a pull request.

@guicattani
Copy link

Been having the same kind of error on my end and came to open a issue, glad you did @nmdoliveira, thanks! This seems to be a change in architecture that wasn't patched yet in this Gem. This issue started January 20th around 6:20 PM (GMT-3) according to our logs.

@joaohornburg
Copy link
Contributor

Thanks @nmdoliveira for reporting this. We'll take a look.

@nmdoliveira
Copy link
Author

Hi! I see this has been resolved in #53, do you plan on making a new release soon @joaohornburg?

@m3nd3s
Copy link
Contributor

m3nd3s commented Jan 12, 2023

Hi @nmdoliveira and @guicattani !

Pelo que percebi são brasileiros, me permitam falar em pt-BR então 😅

O problema ainda persiste para vocês ou podemos dar a issue como resolvida?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants