3

我是新來的Ruby on Rails的&發展同它實現的Facebook連接的Web應用程序。我使用omniauth & fb_graph寶石驗證用戶&獲取用戶的信息。如何在ruby中一次處理不同的FbGraph異常?

雖然發佈用戶對我們的網站後,以他的Facebook牆上,我面對兩種不同的使用情況問題:

  1. 如果用戶更新在我的應用程序複製狀態消息我得到以下異常

    FbGraph::Unauthorized (OAuthException :: (#506) Duplicate status message): 
    
  2. 如果假設用戶改變了他的Facebook密碼,然後這是我從omniauth獲得用戶的訪問令牌變得無效&我得到異常

    以下210
    FbGraph::Unauthorized (OAuthException :: Error validating access token: Session 
    does not match current stored session. 
    This may be because the user changed the password since the time the session 
    was created or Facebook has changed the session for security reasons.): 
    

我目前做this,但我想執行兩個不同的充例外兩個不同的動作。如果狀態消息是重複的,它應該處理第一個excepion。

在第二個例外,應該問的Facebook帳戶重新連接到我的應用程序,這樣我可以得到新的訪問令牌。

請幫助。 謝謝。

回答

3

按照documentation,除應具備codetypemessage屬性。你可以告訴決定採取基於該行動的那些屬性的一個(所有異常message,使一般是有用的,但你會發現,codetype在這種特定情況下更好。

begin 
    # log in and post comment 
rescue FbGraph::Unauthorized => e 
    case e.message 
    when /Duplicate status message/ 
    # handle dup code 
    when /Error validating access token/ 
    # handle bad credentials 
    else 
    raise e 
    end 
end