Rescuing HTTPClient Exceptions

I have a need in a rails app to rescue just exceptions raised due to fairly common situations in downloading data from URLs. Catching all exceptions is bad, as exceptions in my code are treated the same as connection errors, and not passed to my standard exception handling (HTTP 500, plus email to me).

From looking at the docs, there are two base-error classes HTTPClient raises due to http connection errors, BaseResponseError and TimeoutError (these are the two classes that extend RuntimeError). A bunch of others derive from these. I decided not to catch the ConfigurationError.

Adding in a catch for invalid domains / IP addresses, and my rescue statement looks like this:

rescue Errno::EADDRNOTAVAIL, Errno::ETIMEDOUT, HTTPClient::BadResponseError, HTTPClient::TimeoutError

Leave a Reply

Your email address will not be published. Required fields are marked *