Question

I am using open-uri and the seeds.db file. I'm trying to run the following code using "rake db:seed --trace":

CSV.foreach("vendor/users1.csv", { :col_sep => ",",  :quote_char => "\"", :headers => true}) do |row|

user = User.find_by_email("sample#{i}@foobardne.com")

    if (!user.picture_url.nil?)
        begin
            file = open user.picture_url
            user.avatar = file
        rescue OpenURI::HTTPError => error
            user.avatar = nil
        rescue OpenURI::HTTPRedirect => redirect
            user.avatar = nil
        end
            user.password = user.password_confirmation = "foobar"
            user.save
    end

For some reason, every time I run the command, its not recognizing my second rescue statement for the redirect, and I'm getting the following output (which I shortened the end of):

** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
rake aborted!
redirection forbidden:     http://cellbio.med.harvard.edu/faculty/artavanis/images/artavanis4_2002.jpg -> https://cellbio.med.harvard.edu/faculty/artavanis/images/artavanis4_2002.jpg

Any idea why my code isn't catching the redirection error? Any help is truly appreciated!

Was it helpful?

Solution 2

Found the problem. There is a patch to the open-uri.rb file in the ruby library.
Replace:

(/\A(?:http|ftp)\z/i =~ uri1.scheme && /\A(?:http|ftp)\z/i =~ uri2.scheme)

With:

(/\A(?:https?|ftp)\z/i =~ uri1.scheme && /\A(?:https?|ftp)\z/i =~ uri2.scheme)

in the open-uri.rb file. The problem is with http -> https redirects. This file was found for me in:

/Users/MyName/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/open-uri.rb

OTHER TIPS

It doesn't look like the error being thrown is OpenURI::HTTPRedirect. Try this to check which error is getting triggered:

begin
  ...
rescue => e
  puts e.inspect
end

if the error was StandardError then puts e.inspect would return:

#<StandardError: StandardError>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top