title: Trying to rescue HTTParty connection error in Rails 5
body: <p>I am using Rails 5 and HTTParty.</p>
  
  <p>Problem is that if the API is down for some reason my app crashes. I want to handle this case and have found several solutions but did not make them work.</p>
  
  <p>What I did is:</p>
  
  <pre><code>begin
     api = HTTParty.get(URL)
  rescue HTTParty::Error
     puts('API not available')
  end
  </code></pre>
  
  <p>What I hoped is that if the API is down or if I use an invalid URL it would not give me an internal server error but tell me the the API is not available and continue with the code.</p>
  
  <p>But instead it gives and internal server error:</p>
  
  <pre><code>SocketError (Failed to open TCP connection to URL (getaddrinfo: nodename nor servname provided, or not known)):
  </code></pre>
  
  <p>How can I make sure that this does not happen?</p>