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
A better practice here will be to rescue the exception you want, instead of
StandardError
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 aboveFigured 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.
Oli’s reply pointed me in the right direction, this one did the job for me:
then check for nil.