lib/api_cache/api.rb in api_cache-0.2.1 vs lib/api_cache/api.rb in api_cache-0.2.2

- old
+ new

@@ -31,22 +31,22 @@ # This method can raise Timeout::Error, APICache::InvalidResponse, or any # exception raised in the block passed to APICache.get # def get check_queryable! - APICache.logger.debug "Fetching data from the API" + APICache.logger.debug "APICache #{@key}: Calling API" set_queried_at Timeout::timeout(@timeout) do if @block # If this call raises an error then the response is not cached @block.call else get_key_via_http end end rescue Timeout::Error => e - raise APICache::TimeoutError, "Timed out when calling API (timeout #{@timeout}s)" + raise APICache::TimeoutError, "APICache #{@key}: Request timed out (timeout #{@timeout}s)" end private def get_key_via_http @@ -54,12 +54,16 @@ case response when Net::HTTPSuccess # 2xx response code response.body else - raise APICache::InvalidResponse, "InvalidResponse http response: #{response.code}" + raise APICache::InvalidResponse, "APICache #{@key}: InvalidResponse http response: #{response.code}" end + rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, + Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, + Net::ProtocolError, Errno::ECONNREFUSED, SocketError => e + raise APICache::InvalidResponse, "APICache #{@key}: Net::HTTP error (#{e.message} - #{e.class})" end def redirecting_get(url) r = Net::HTTP.get_response(URI.parse(url)) r.header['location'] ? redirecting_get(r.header['location']) : r @@ -69,17 +73,17 @@ # since the last query to the API). # def check_queryable! if previously_queried? if Time.now - queried_at > @period - APICache.logger.debug "Queryable: true - retry_time has passed" + APICache.logger.debug "APICache #{@key}: Is queryable - retry_time has passed" else - APICache.logger.debug "Queryable: false - queried too recently" + APICache.logger.debug "APICache #{@key}: Not queryable - queried too recently" raise APICache::CannotFetch, "Cannot fetch #{@key}: queried too recently" end else - APICache.logger.debug "Queryable: true - never used API before" + APICache.logger.debug "APICache #{@key}: Is queryable - first query" end end def previously_queried? APICache.store.exists?("#{@key}_queried_at")