Top Level Namespace

Defined Under Namespace

Modules: DevTools, MetaProject, PathnameIterator, Rake Classes: Pathname, String

Instance Method Summary collapse

Instance Method Details

#better_open(url, headers = nil, retries = 2, &block) ⇒ Object

Wrapper around Kernel.open from open-uri that can retry and report better errors



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/meta_project/core_ext/open_uri.rb', line 5

def better_open(url, headers=nil, retries=2, &block)
  begin
    # open-uri's open
    headers.nil? ? Kernel.open(url, &block) : Kernel.open(url, headers, &block)
  rescue Errno::ECONNREFUSED, EOFError => e
    if(retries > 0)
      STDERR.puts "Connection refused to #{url} - retrying in 1 sec."
      sleep 1
      better_open(url, headers, retries-1, &block)
    else
      e.message << " (URL:#{url})"
      raise e
    end
  rescue OpenURI::HTTPError => e
    e.message << " (URL:#{url})"
    raise e
  end
end