skip to Main Content

I am trying to catch an exception, when a certain id is not found, but the App still stops with the message:
ActiveResource::ResourceNotFound

The Code is the following:

begin
  ShopifyAPI::ScriptTag.find(x.scriptid)
rescue => e
  if e.message == '404 Not Found'
  # handle 404 error
  else
    raise e
  end
end

Did I do something wrong?

3

Answers


  1. A better practice here will be to rescue the exception you want, instead of StandardError

    rescue ActiveResource::ResourceNotFound => e
      # handle 404 error
    end
    

    I can’t say right away why your example doesn’t work, but I guess that the message is not exactly 404 Not Found

    You can use regex in this case e.message.match?(/404 Not Found/), but I’d prefer the approach above

    Login or Signup to reply.
  2. Figured this one out as it’s been pestering me too.
    You can ask the Shopify API for a collection by passing in :all and a param of the ids. Then just raise an error manually if it returns an empty array.

    begin
      @product = ShopifyAPI::Product.find(:all, params: { ids: product_id })&.first || raise('Shopify product not found')
    rescue => e
      puts e.message and return false
    end
    
    Login or Signup to reply.
  3. Oli’s reply pointed me in the right direction, this one did the job for me:

    ShopifyAPI::Product.all(ids: member.shopify_id).first
    

    then check for nil.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search