Ad

Shopify: Catching Exceptions Of RestAPI Calls From The ShopfyAPI

- 1 answer

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?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad