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 4093558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:37:59+00:00 2026-05-20T19:37:59+00:00

What’s the best way to rescue exceptions from Net::HTTP? Exceptions thrown are described in

  • 0

What’s the best way to rescue exceptions from Net::HTTP?

Exceptions thrown are described in Ruby’s socket.c, like Errno::ETIMEDOUT, Errno::ECONNRESET, and Errno::ECONNREFUSED. The base class to all of these is SystemCallError, but it feels weird to write code like the following because SystemCallError seems so far removed from making an HTTP call:

begin
  response = Net::HTTP.get_response(uri)
  response.code == "200"
rescue SystemCallError
  false
end

Is it just me? Is there a better way to handle this beyond fixing Net::HTTP to handle the Errno exceptions that would likely pop up and encapsulate them in a parent HttpRequestException?

  • 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-20T19:38:00+00:00Added an answer on May 20, 2026 at 7:38 pm

    I agree it is an absolute pain to handle all the potential exceptions. Look at this to see an example:

    Working with Net::HTTP can be a pain. It’s got about 40 different ways
    to do any one task, and about 50 exceptions it can throw.

    Just for the love of google, here’s what I’ve got for the “right way”
    of catching any exception that Net::HTTP can throw at you:

    begin
      response = Net::HTTP.post_form(...) # or any Net::HTTP call
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
      ...
    end
    

    Why not just rescue Exception => e? That’s a bad habit to get into, as
    it hides any problems in your actual code (like SyntaxErrors, whiny
    nils, etc). Of course, this would all be much easier if the possible
    errors had a common ancestor.

    The issues I’ve been seeing in dealing with Net::HTTP have made me
    wonder if it wouldn’t be worth it to write a new HTTP client library.
    One that was easier to mock out in tests, and didn’t have all these
    ugly little facets.

    What I’ve done, and seen most people do, is move away from Net::HTTP and move to 3rd party HTTP libraries such as:

    httparty and faraday

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

Sidebar

Related Questions

Best way to extract the minutes and seconds from youtube urls? http://www.youtube.com/watch?v=TjTbNWhsG28#t=1m40s&foo=1&bar=2 where 1m40s
The best way to do this? Tried things like that: public String FormatColumnName(String columnName)
the best way center 2 divs inner div. Like this 1 2 3 4
What is the best way to construct an #ask query using semantic properties from
Best way to get notification in from gmail in C#, when new email is
The best way to remove duplicate values ( NSString ) from NSMutableArray in Objective-C?
Best way to Handle Exceptions in C# Catch Block.I have no other choice but
The best way to take a string that is formated like... YYYY-MM-DD and make
Best way to Install RUBY 1.9, Rails 3, Postgres, Heroku, GitHub, Using RVM Can
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html

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.