Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4346592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:11:37+00:00 2026-05-21T12:11:37+00:00

I hacked this together as a seemingly robust way to call a flaky webservice

  • 0

I hacked this together as a seemingly robust way to call a flaky webservice that was giving timeouts and the occasional name resolution or socket error or whatever.
I thought I’d put it here in case it’s useful or, more likely, to be told a better way to do this.

require 'net/http'

retries = 5
begin
  url = URI.parse('http://api.flakywebservice.com')
  http = Net::HTTP.new(url.host, url.port)
  http.read_timeout = 600  # be very patient
  res = nil
  http.start{|http|
    req = Net::HTTP::Post.new(url.path)
    req.set_form_data(params)  # send a hash of the POST parameters
    res = http.request(req)
  }
rescue Exception   # should really list all the possible http exceptions
  sleep 3
  retry if (retries -= 1) > 0
end

# finally, do something with res.body, like JSON.parse(res.body)

The heart of this question is:
What all exceptions should I be looking for when making a call to a webservice like this?
Here’s an attempt to collect them all, but it seems like there’s got to be a better way than that:
http://tammersaleh.com/posts/rescuing-net-http-exceptions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:11:37+00:00Added an answer on May 21, 2026 at 12:11 pm

    Exceptions are meaningful, and Net::HTTP offers specific exceptions for different sorts of cases. So if you want to handle them each in a particular way, you can.

    That article says that handling those specific exceptions is better/safer than handling rescue Exception, and that’s very true. BUT, rescue Exception is different from rescue by itself, which is the equivalent to rescue StandardError, which is what you should usually do by default if you don’t have a reason to do anything else.

    Rescuing top-level Exception will rescue anything that could possibly happen in the entire execution stack, including some part of ruby running out of disk or memory or having some obscure system-related IO problem.

    So, as far as “what to rescue”, you’re generally better off if you change your code to rescue. You’ll catch everything you want to, and nothing that you don’t want to. However, in this particular case, there is one lone exception in that guy’s list that is NOT a descendent of StandardError:

    def parents(obj)
      ( (obj.superclass ? parents(obj.superclass) : []) << obj)
    end
    
    [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse,
      Net::HTTPHeaderSyntaxError, Net::ProtocolError].inject([]) do |a,c|
      parents(c).include?(StandardError) ? a : a << c
    end
    # Timeout::Error < Interrupt
    
    parents(Timeout::Error)
    # [ Object, Exception < Object, SignalException < Exception,
    #   Interrupt < SignalException, Timeout::Error < Interrupt ]
    

    So you could change your code to rescue StandardError, Timeout::Error => e and you’ll cover all the cases mentioned in that article, and more, but not the stuff that you don’t want to cover. (the => e is not required, but more on that below).

    Now, as far as your actual technique for dealing with the flakey API — the question is, what’s the problem with the API that you are dealing with? Badly formatted responses? No responses? Is the problem at the HTTP level or in the data you are getting back?

    Maybe you don’t yet know, or you don’t yet care, but you know that retrying tends to get the job done. In that case, I would at least recommend logging the exceptions. Hoptoad has a free plan, and has some sort of thing like Hoptoad.notify(e) — I can’t remember if that’s the exact invocation. Or you can email it or log it, using e.message and e.stacktrace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v4.0. Update: I've hacked together this so far: public class AllFilter :
I hate code that looks like its been hacked together. I have just written
So I'm not sure how to do this in a cakey way. I've hacked
I've hacked together this code with various if and elseif statement's and just wondering
This has to be the biggest nuisance I've encountered with PyQT: I've hacked together
This partial code I hacked in to JamieMThomas's JQuery plugin that combines the Microsoft
I've hacked together something that works flawlessly but it's really ugly code and I
I found this code on a hacked site, what does it do exactly? It
Ive got the following code which I hacked together from website examples. It works
In a custom module for drupal 4.7 I hacked together a node object and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.