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

Handle html errors. #47

Closed
omar10594 opened this issue Aug 8, 2019 · 4 comments · Fixed by #48
Closed

Handle html errors. #47

omar10594 opened this issue Aug 8, 2019 · 4 comments · Fixed by #48
Assignees
Labels

Comments

@omar10594
Copy link

Hi, at our company we used your library to add contacts to our RD Station account when they signup in our site, sometimes the api responds with an error, but it's an html error so we are getting a JSON parse error.

I think this type of errors occurs when the api has a problem (status.rdstation.com.br)

Expected Behavior

Should raise a valid error

Current Behavior

Raises a JSON::ParserError

Failure Information

/usr/lib/ruby/2.5.0/json/common.rb:156 parse
/usr/lib/ruby/2.5.0/json/common.rb:156 parse
rdstation-ruby-client (1.2.1) lib/rdstation/api_response.rb:4 build
rdstation-ruby-client (1.2.1) lib/rdstation/contacts.rb:52 upsert

This is an example of the body when we get a html error response.

HTTP/1.1 504 Bad Gateway Timeout
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title> RD Station fora do ar </title>
  <link rel="stylesheet" media="screen" href="//d3ndvx6e67vt0s.cloudfront.net/assets/application-d375e73fcd0597fba6493e937b68d993342fd448188d11e28800f0d6df119d0f.css" />
  <link rel="stylesheet" media="screen" href="//d3ndvx6e67vt0s.cloudfront.net/assets/error_pages-6abdb40a29bf6cb060cddd481fe045a252c19093fe907322be26435ac06a5209.css" />
  <script type="text/javascript">
    ACCOUNT_LOCALE = "pt-BR";
    LOCALE_MAP = JSON.parse('{"pt-BR":"pt_BR","en":"en","es":"es"}');
    LOCALE_INFO = JSON.parse('[{"locale":"pt-BR","name":"Português"},{"locale":"en","name":"English"},{"locale":"es","name":"Español"}]');
  </script>
  <script src="//d3ndvx6e67vt0s.cloudfront.net/assets/i18n-5d5e12e593d28eef259ad0e70f4679c16f5ce94f882e6b39c84a6083a35ba200.js" cache="true"></script>
  <script src="//d3ndvx6e67vt0s.cloudfront.net/assets/i18n/locale-83031e493a698e87814ddd84c66e5d73648b7a4e797c83333314094489a96eeb.js" cache="true"></script>
  <script src="//d3ndvx6e67vt0s.cloudfront.net/assets/i18n/frontend-global-a346ede1965d7252767c63c67eb9a815613c206c70c7616ef75194626ee43b20.js" cache="true"></script>
  <script src="//d3ndvx6e67vt0s.cloudfront.net/assets/i18n/frontend-pt-BR-984c7fbf367fa642586b7612b773a9d2f36e009c72b1a9d4a9f9e5e8ea157c46.js" cache="true"></script>

</head>

<body class="bg-purple">
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container">
    <div class="row">
      <div class="col-xs-12 text-center">
        <h1 id="rdstation-logo" class="center-logo">
          <a href="/home" title="Início">
            <svg id="logoRDStationMarketing" class="brand-rdstation-logo brand-rdstation-mkt-logo" width="200px" fill="white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 311 40.74">
              <path class="pathLogo" d="M200.69,21.71c-.13-2.39-.28-5.26-.25-7.4h0c-.59,2-1.31,4.17-2.18,6.55l-3,8.37h-1.69L190.69,21c-.82-2.43-1.51-4.66-2-6.71h0c-.05,2.14-.18,5-.33,7.58l-.46,7.35h-2.11L187,12.13h2.86l2.91,8.25c.71,2.1,1.29,4,1.72,5.74h.08c.43-1.72,1-3.59,1.8-5.74l3-8.25h2.86l1.07,17.11h-2.16Z"/>
              <path class="pathLogo" d="M209.5,23.95l-1.75,5.29h-2.37l5.81-17.11h2.66l5.84,17.11h-2.44l-1.8-5.29Zm5.57-1.87-1.68-4.95C213,16,212.75,15,212.49,14h-.05c-.26,1-.54,2.07-.87,3.11l-1.68,5Z"/>
              <path class="pathLogo" d="M222.05,12.3a23.87,23.87,0,0,1,4.33-.36c2.41,0,4,.43,5.05,1.4a4.24,4.24,0,0,1,1.37,3.26A4.52,4.52,0,0,1,229.52,21v0c1.37.46,2.18,1.69,2.6,3.48a26.81,26.81,0,0,0,1.37,4.73h-2.37a21.88,21.88,0,0,1-1.18-4.12c-.53-2.38-1.47-3.27-3.55-3.35h-2.16v7.46h-2.18Zm2.18,7.91h2.27c2.37,0,3.87-1.3,3.87-3.26,0-2.21-1.6-3.18-3.95-3.21a9.11,9.11,0,0,0-2.19.2Z"/><path class="pathLogo" d="M235.74,12.13h2.18v8.4H238c.46-.67.93-1.29,1.37-1.86l5.32-6.54h2.49l-6.11,7.26,6.58,9.85h-2.58l-5.56-8.4-1.6,1.88v6.52h-2.18Z"/>
              <path class="pathLogo" d="M258.44,21.15h-6.53v6.22h7.46v1.87h-9.64V12.13h9.33V14h-7.15v5.29h6.53Z"/>
              <path class="pathLogo" d="M265.9,14h-5.29V12.13h12.75V14h-5.29V29.23H265.9Z"/>
              <path class="pathLogo" d="M277.41,12.13V29.23h-2.18V12.13Z"/>
              <path class="pathLogo" d="M281.14,29.23V12.13h2.15L289,20.78a47.76,47.76,0,0,1,3.21,5.56l.05,0c-.21-2.28-.27-4.37-.27-7V12.13h2.18V29.23h-2l-5.68-8.68a53.61,53.61,0,0,1-3.34-5.71l-.08,0c.13,2.16.19,4.21.19,7.06v7.31Z"/>
              <path class="pathLogo" d="M311,28.46a16.17,16.17,0,0,1-5.3.94,8.91,8.91,0,0,1-6.45-2.27,8.8,8.8,0,0,1-2.41-6.39c0-5.12,3.56-8.86,9.35-8.86a10.73,10.73,0,0,1,4.3.79l-.54,1.81a9,9,0,0,0-3.82-.74c-4.2,0-6.94,2.6-6.94,6.9s2.64,6.92,6.66,6.92a7.31,7.31,0,0,0,3-.46v-5H305.4V20.21H311Z"/>
              <g id="7a7b2626-ee12-48d4-a845-88dd91af2494" data-name="Logo RD Station">
                <path class="pathLogo" d="M0,5.16a47.65,47.65,0,0,1,7.28-.51c3.59,0,6.1.54,7.82,1.92a6.22,6.22,0,0,1,2.22,5.1,6.47,6.47,0,0,1-4.27,6v0c1.72.69,2.67,2.33,3.29,4.58.77,2.76,1.53,6,2,6.91H12.66a30.61,30.61,0,0,1-1.72-5.71c-.66-3.06-1.72-3.89-4-3.93H5.29v9.64H0ZM5.6,15.55H7.77c2.75,0,4.38-1.38,4.38-3.51S10.63,8.67,8.1,8.64a13.81,13.81,0,0,0-2.5.18Z"/>
                <path class="pathLogo" d="M20.53,5.15A49.24,49.24,0,0,1,28,4.64c4.63,0,7.64.83,10,2.61,2.53,1.88,4.13,4.89,4.13,9.19,0,4.67-1.7,7.89-4.05,9.88-2.57,2.14-6.48,3.15-11.26,3.15a47.73,47.73,0,0,1-6.26-.36Zm5.29,19.91a10.14,10.14,0,0,0,1.92.11c5,0,8.25-2.72,8.25-8.54,0-5.07-2.93-7.75-7.67-7.75a11.47,11.47,0,0,0-2.5.22Z"/>
                <path class="pathLogo" d="M49.5,25.42A11.06,11.06,0,0,0,55.1,27c3.19,0,5.06-1.69,5.06-4.13,0-2.26-1.29-3.55-4.56-4.81-3.95-1.4-6.39-3.44-6.39-6.85,0-3.77,3.12-6.57,7.82-6.57a11,11,0,0,1,5.35,1.18l-.86,2.55a9.63,9.63,0,0,0-4.59-1.15c-3.3,0-4.56,2-4.56,3.62,0,2.26,1.47,3.37,4.81,4.66,4.09,1.58,6.17,3.55,6.17,7.1,0,3.73-2.76,7-8.47,7A12.49,12.49,0,0,1,48.71,28Z"/>
                <path class="pathLogo" d="M69.66,7.8v4h4.66v2.49H69.66v9.45c0,2.17.61,3.4,2.39,3.4a7.08,7.08,0,0,0,1.84-.22L74,29.33a7.77,7.77,0,0,1-2.82.43,4.41,4.41,0,0,1-3.44-1.34c-.9-.94-1.23-2.5-1.23-4.56V14.31h-2.8V11.82h2.8V8.64Z"/>
                <path class="pathLogo" d="M96.41,7.8v4h4.67v2.49H96.41v9.45c0,2.17.62,3.4,2.39,3.4a7.08,7.08,0,0,0,1.84-.22l.14,2.39a7.78,7.78,0,0,1-2.82.43,4.41,4.41,0,0,1-3.44-1.34c-.9-.94-1.23-2.5-1.23-4.56V14.31H90.5V11.82h2.8V8.64Z"/>
                <path class="pathLogo" d="M87,29.23l-.25-2.18h-.11a6.45,6.45,0,0,1-5.29,2.57,4.93,4.93,0,0,1-5.29-5c0-4.17,3.72-6.46,10.4-6.42v-.36c0-1.43-.39-4-3.93-4A8.63,8.63,0,0,0,78,15.17l-.71-2.07A10.75,10.75,0,0,1,83,11.57c5.29,0,6.58,3.6,6.58,7.07v6.46a24.24,24.24,0,0,0,.29,4.14Zm-.54-9C83,20.21,79,20.82,79,24.23a2.82,2.82,0,0,0,3,3,4.36,4.36,0,0,0,4.24-2.93,3.32,3.32,0,0,0,.18-1Z"/>
                <path class="pathLogo" d="M107.36,6.78a2,2,0,0,1-3.95,0,1.94,1.94,0,0,1,2-2,1.89,1.89,0,0,1,2,2m-3.49,5H107V29.23h-3.11Z"/>
                <path class="pathLogo" d="M126.52,20.32c0,6.48-4.49,9.3-8.72,9.3-4.74,0-8.4-3.48-8.4-9,0-5.86,3.84-9.3,8.69-9.3,5,0,8.43,3.66,8.43,9m-13.9.18c0,3.84,2.21,6.73,5.32,6.73s5.32-2.86,5.32-6.81c0-3-1.48-6.73-5.25-6.73s-5.39,3.47-5.39,6.8"/>
                <path class="pathLogo" d="M128.75,16.53c0-1.8,0-3.27-.14-4.71h2.7l.17,2.8h.22a6.44,6.44,0,0,1,5.79-3.29c2.42,0,6.19,1.45,6.19,7.45V29.23h-3.42V19.13c0-2.82-1-5.18-4-5.18a4.46,4.46,0,0,0-4.2,3.26,4.65,4.65,0,0,0-.21,1.48V29.23h-3.11Z"/>
              </g>
              <rect class="pathLogo" x="165.14" width="1" height="40"/>
            </svg>

          </a>
        </h1>
      </div>
    </div>
  </div>
</nav>

<div id="content-outside" class="stars">

  <div class="box_satellite objects">
    <img width="300" class="object_satellite" src="//d3ndvx6e67vt0s.cloudfront.net/assets/error_page/satellite-89cec547c4f0fa71e51de6562a666134e4406a0c00eb2f7bdfe88a07f33a0ff4.svg" alt="Satellite" />
  </div>

  <section class="section-heading text-center container">
    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <h1>Oops</h1>
        <h4>Ocorreu um erro ao tentar acessar esse conteúdo. Sentimos muito!</h4>
        <p>Recebemos um aviso e em breve resolveremos essa situação.</p>
        <p>Você ainda pode buscar auxílio em nossa <a href="https://ajuda.rdstation.com.br/hc/pt-br/" title="Central de Ajuda">Central de Ajuda</a>.</p>
      </div>
    </div>
  </section>


  <section class="section-body">
    <div class="objects">
      <div class="earth-moon">
        <img width="110" class="object_earth" src="//d3ndvx6e67vt0s.cloudfront.net/assets/error_page/earth-ded68340ce6ce9a267e01cf8a018efd55417b09ac1623aec4bfa59af839e02e2.svg" alt="Earth" />
        <img width="40" class="object_moon" src="//d3ndvx6e67vt0s.cloudfront.net/assets/error_page/moon-8110bbc1f2d6f838281f3a004eb931d23f55e6cd1dc39b1964e88e3b033186d0.svg" alt="Moon" />
      </div>

    </div>
    <div class="glowing_stars">
      <div class="star"></div>
      <div class="star"></div>
      <div class="star"></div>
      <div class="star"></div>
      <div class="star"></div>
    </div>

    <div class="row">
      <div class="col-sm-8 col-sm-offset-2">
        <p>Você gostaria de ir para outra página?</p>
        <a class="btn btn-links" href="/leads">Base de Leads</a>
        <a class="btn btn-links" href="/landing-pages">Landing Pages</a>
        <a class="btn btn-links" href="/campanhas">Email Marketing</a>
        <a class="btn btn-links" href="/configuracoes">Configurações</a>
      </div>
    </div>
  </section>
</div>
</body>
</html>

For this example as it is a 504 code error, should raise a RDStation::Error::ServerError

Solution

I think here the status code could be checked before parse the body.

def self.build(response)
response_body = JSON.parse(response.body)
return response_body if response.code.between?(200, 299)
RDStation::ErrorHandler.new(response).raise_error
end

or for example return JSON.parse(response.body) if response.code.between?(200, 299)

And do something similar in the error handler class to avoid parse the response there, or add a rescue block to return an empty errors array when the body could not be parsed.

With our logs we only can see the exception, not the request, so I'm not sure about the headers that it includes, but maybe the content type could be checked to know if it is a json body or not.

@joaohornburg
Copy link
Contributor

Hi @omar10594 thanks for submitting this issue. We'll take a look.

@joaohornburg
Copy link
Contributor

@omar10594 are you able to reproduce this error? What is the HTTP response? You can see the raw HTTPS this way: https://gist.github.com/joaohornburg/27eb021709cda0bc4a38

@omar10594
Copy link
Author

Hi, I will try to log the response the next time we get a similar error, the last time we get an error of this type was at August 9, looks like they were having a problem in some of their services (http://status.rdstation.com.br/pages/history/54da143e240e245d4d000009#5d4ad79041918319d7d13f78)

@joaohornburg
Copy link
Contributor

@omar10594 #48

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

Successfully merging a pull request may close this issue.

2 participants