This week at work we struggled to find a nice way to rescue an exception (OpenURI::HTTPError) in a way that doesn’t look terrible. Digging into the internet, there wasn’t much information so I decided to post our solution here, it may be not the perfect one, but it was the best we could get…
In our context, from time to time, we go to a url and check if it’s already available, otherwise we get a 404, which is perfectly expected in our case. To avoid NewRelic to warn us every single time with this error, we decided to catch only the 404 error and try again (it’s a Sidekiq job) until a certain amount of times.
In this way, we’re able to add this class FileNotReady as an exception for NewRelic.
If you go the the OpenURI::HTTPError
documentation you can see that there are two arguments: message and io, the last one is the one we need to check the code. In our case, message would be 404 Not found.
How can we test this?
Hope this can help you! Cheers.