0

I am learning more and more about ruby and have written a script that times out on occassion (making several API calls).

I know I need to rescue the exception with something like this:

rescue Timeout::Error => e
  # log #{e} for later, maybe
end

My question is where I should place that rescue block. Since I am making several API calls within multiple loops, would I need to put that block within each API loop? I would prefer to just write the rescue block once, at the end of the script for example, and have it work inside each loop in the script. I am using ruby 1.9.3.

Luigi
  • 5,443
  • 15
  • 54
  • 108
  • take Avdi's book and go though that... – Arup Rakshit Aug 28 '13 at 14:09
  • 2
    I think the answer is that it depends upon how precise you want to be about locating when/where the exception occurred. For example, if you want to see a message that pinpoints which API call timed out, then you need a `rescue` for each one. – lurker Aug 28 '13 at 14:11

1 Answers1

1

It mostly depends of what do you try to do with the errors.

For example, if you want the errors not to abort the loop run, catch the exception inside the loop, you can log the error and continue with the next iteration.

If the error is kind of "fatal" and you cannot (or don't want to) continue with the loop, you catch it outside.

DaniDoni
  • 11
  • 3
  • That makes sense. I'm scheduling ~10,000 emails through the script, and it's important that all 10,000 of them make it where they need too. I suppose I'll catch the exception outside the loop and force it to delete all the scheduled emails, then I can rerun the script to re-schedule them all. Doesn't seem like the most efficient idea but it will suffice for now. Thanks for the help! – Luigi Aug 28 '13 at 14:18