Variables not recognized within Rescue in Ruby -
i have following code:
rescue timeout::error, standarderror => e puts "caught exception: #{e.message}".red log.puts("#{e.backtrace}") email_ids_all.each |email_delete| call= "/api/v2/emails/#{email_delete}/" ...... before rescue piece have defined log , email_ids_all. however, neither of these recognized within ruby script. if this:
rescue timeout::error, standarderror => e file.open(rescuelogfile, 'w') |log| #setup log write response codes. puts "caught exception: #{e.message}".red log.puts("#{e.backtrace}") email_ids_all.each |email_delete| call= "/api/v2/emails/#{email_delete}/" .... log works fine, makes sense. take lot of writing redefine email_ids_all array , other variables contained inside rescue block.
is there anyway allow variables recognized inside rescue? code laid out this:
begin #some code rescue #above code end i using ruby 1.9.3.
edit----
log starts right after begin statement :
begin file.open(logfile, 'w') |log| #setup log write response codes. log.puts works throughout entire code except when error thrown, , runs rescue script log not available.
the same goes email_ids_all. there api call generates 10,000 emails , each of them added array email_ids_all. script receiving error halfway through generating these emails, , need rescue script delete email ids in email_ids_all array. whatever reason, following error:
fs_test_env.rb:762:in `block in <main>': undefined local variable or method `email_ids_all' main:object (nameerror) fs_test_env.rb:759:in `open' fs_test_env.rb:759:in `rescue in <main>' fs_test_env.rb:7:in `<main>' any thoughts?
the scope of block parameter log limited block. whole point of open block.
maybe want do:
begin log = file.open('logfile', 'w') ... rescue ... ensure log.close end note not cover errors when opening logfile.
regarding email_ids_all, guess (!) have exception in statement like:
email_ids_all = ... long , complex calculation raises exception if yes, problem assignment happens after whole right-hand side calculated. var email_ids_all not yet created when exception happens.
in order access elements created before exception, have keep track of them, e.g.
begin email_ids = [] 10000.times email_ids << ... # create element raising exception end rescue ... # treat created elements end
Comments
Post a Comment