2010-04-21 50 views
1

我有一段很有用的代碼。我試圖推文相同的文本,我的劇本結束,因爲/lib/ruby/gems/1.8/gems/twitter-0.9.4/lib/twitter.rb:87:in 'raise_errors': (403): Forbidden - Status is a duplicate. (Twitter::General)如何處理與twitter.com溝通的「錯誤」? (ruby + twitter gem)

我知道我不能推特相同的文本兩次,但我想我會得到響應變量內的錯誤。

我該如何處理錯誤?所以我的腳本會很好地完成,而不是因爲錯誤?

oauth = Twitter::OAuth.new('consumer token', 'consumer secret') 
oauth.authorize_from_access('access token', 'access secret') 

client = Twitter::Base.new(oauth) 
response = client.update('Heeeyyyyoooo from Twitter Gem!') 

回答

2

你可以用陳述的任何紅寶石陳述或塊begin .. rescue .. end發現錯誤 - 你可能想試試這個:

begin 
    oauth = Twitter::OAuth.new('consumer token', 'consumer secret') 
    oauth.authorize_from_access('access token', 'access secret') 

    client = Twitter::Base.new(oauth) 
    response = client.update('Heeeyyyyoooo from Twitter Gem!') 
rescue Twitter::General 
    # Catch the error and do nothing 
end 

如果你想捕捉任何錯誤您可以將救援線路更改爲rescue。你可以閱讀更多關於他們on the ruby-doc website

+0

獲得一個錯誤可能是一件壞事,你可能想要解決這個問題而不是壓制錯誤 - 我不太確定它的出現。 – 2010-04-21 14:13:45

+0

@JP:謝謝,我會試試... – Radek 2010-04-21 14:50:00