2013-08-28 78 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