2013-08-28 63 views
1

我有下面的代码行是给我的错误:红宝石救援语法错误

rescue Timeout::Error => e 
    logs.puts("Rescued a timeout error...#{e}") 
    email_ids_all.each do |email_delete| 

     call= "/api/v2/emails/#{email_delete}/" 
     uri= HTTParty.delete("https://www.surveys.com#{call}", 
      :basic_auth => auth, 
      :headers => { 'ContentType' => 'application/x-www-form-urlencoded', 'Content-Length' => "0" } 
    ) 
     puts "Deleted email #{email_delete}".green 
     log.puts("Deleted email #{email_delete}") 
    end 
    abort #abort entire script after deleting emails 
    end 

我收到的错误是这样的:

syntax error, unexpected keyword_rescue, expecting $end 
    rescue Timeout::Error => e 
     ^

从本质上讲,我只是想经营如果脚本超时,则会删除API。虽然我在rescue的块中放入的内容似乎并不重要,但我收到了同样的错误。我的rescue方法的语法有什么问题?

+2

显然你没有'开始'在适当的地方。我不能确切地说,除非你粘贴你的所有代码。 –

+0

也许答案是我根本没有'开始'。这是整个代码... – Luigi

+0

我添加了开始和脚本工作正常 - 不知道为什么downvotes,但感谢您的帮助。随意发表您的评论作为答案,我会给你信用。 – Luigi

回答

16

使用rescue的格式如下:

begin 
    # Code you want to run that might raise exceptions 
rescue YourExceptionClass => e 
    # Code that runs in the case of YourExceptionClass 
rescue ADifferentError => e 
    # Code that runs in the case of ADifferentError 
else 
    # Code that runs if there was no error 
ensure 
    # Code that always runs at the end regardless of whether or not there was an error 
end 

这里是有很多的更多信息一个问题:Begin, Rescue and Ensure in Ruby?

+0

非常有意义 - 感谢您的解释,我只是简单地忽略了'begin'部分。 – Luigi

+1

@LeviStanley值得注意的是,如果你想拯救所有方法代码,你不需要'begin'关键字。 –

+0

你的意思是隐含的异常块(如定义方法时)或其他东西? – 2013-08-28 14:46:15