0

Faraday::TimeoutError in Spree::Products#show , Net::ReadTimeout with #TCPSocket:(closed)

So i am trying to check if the URL is valid and exists, i tried sending request_head with http to get the response, i also did the same with faraday. My probloem is when i try doing that in the console it works perfectly console

Faraday.head("http://localhost:3000/"+url_for(controller: "spree/products", action: "show", id: "creme-mains-nourrissante", locale: 'fr')).status It returns 200 which is what i need

My code is : menu_helper.rb

def url_valid?(url_string)
    Faraday.head(url_string).status == 200
end

 def url_change (lang)
    url = ""
    controller =  Rails.application.routes.recognize_path(request.path_info)[:controller]
    action = Rails.application.routes.recognize_path(request.path_info)[:action]
    
    ... 
    ...
    

    if controller == "spree/products" && action == "show"
      if lang == :en
        i = Spree::Product::Translation.where(spree_product_id: Spree::Product.find_by(slug: params[:id]).id).where.not(slug: params[:id]).ids
        id = Spree::Product::Translation.find(i[0]).slug
        return url_valid?("http://"+request.host_with_port+url_for(controller: "spree/products", action: action, id: id, locale: 'fr')) ?  url_for(controller: "spree/products", action: action, id: id, locale: 'fr') : "http://"+request.host_with_port+"/"+Spree::Taxon.find_by(code: 'best_sales')
      elsif lang == :fr
        i = Spree::Product::Translation.where(spree_product_id: Spree::Product.find_by(slug: params[:id]).id).where.not(slug: params[:id]).ids
        id = Spree::Product::Translation.find(i[0]).slug

  ############# i call the url_valid? in return 
        return url_valid?("http://"+request.host_with_port+url_for(controller: "spree/products", action: action, id: id, locale: 'en')) ?  url_for(controller: "spree/products", action: action, id: id, locale: 'en') : "http://"+request.host_with_port+"/"+Spree::Taxon.find_by(code: 'best_sales')
      end
    end

  end

I also tried :

   require "net/http"
    url = URI.parse("http://www.google.com/")
    req = Net::HTTP.new(url.host, url.port)
    res = req.request_head(url.path)

After waiting for a while i get a timeout error, Faraday::TimeoutError in Spree::Products#show , Net::ReadTimeout with #TCPSocket:(closed)

error_screenshot

Any help will be appreciated, thanks

Niss
  • 1
  • 3
  • `"http://localhost:3000/"+url_for(controller: "spree/products", action: "show", id: "creme-mains-nourrissante", locale: 'fr')` will create an invalid URL since it will contain the protocol and host twice : `http://localhost:3000/http://localhost:3000/...`. Just use `url_for` to create the entire URL. – max May 10 '22 at 11:54
  • If you actually wanted to construct a URL manually use `path_for` together with the URI module instead of string concatention. Beyond that this is very strange solution - are you sure this isn't an X Y Problem? – max May 10 '22 at 11:55
  • The URL is valid, i run the exact thing in cobsole and i get the right URL. My only problem is the timeout, i figured maybe it's because it tries to run a get and head at the same time so it kinda get blocked? i'm not sure tho – Niss May 10 '22 at 12:31

0 Answers0