2016-01-18 32 views
0

目前,我正在使用api_auth寶石。我在一個函數中通過發出一個簽名請求來實現外部API。目前我的代碼看起來像這樣。如何在使用api_auth gem時處理錯誤?

access_id = "someKey" 
secret_key = "someRandomGeneratedKey" 
request = RestClient::Request.new(url: 'serverUrl', method: get, headers:'headers') 

@signed_request = ApiAuth.sign!(access_id, secret_key, request) 
@signed_request.execute 

我無法將任何錯誤處理代碼應用於執行請求的最後一條語句。有什麼建議麼?

回答

0

你必須使用開始救援塊來處理異常。

begin 
    #TODO Exception occurring statement 
rescue 
    #Exception handling code 
end 

由於您應該只救出特定的異常,而不是全部。 所以作爲你的代碼建議你使用rest_client & api-auth寶石,所以從文檔你可以得到這個寶石異常列表。

示例 - 對於rest_client以下的異常需要處理。 (也許這是你的問題的解決方案,只需更換最後一行下面給出)

begin 
    @signed_request.execute 
rescue RestClient::ExceptionWithResponse, URI::InvalidURIError, RestClient::InternalServerError => err 
    p err 
end 

很少有例外也api-auth寶石產生也喜歡 ApiAuth::ApiAuthErrorApiAuth::UnknownHTTPRequest所以你需要拯救這一切的例外。

參考鏈接 -

http://www.rubydoc.info/gems/api-auth/1.4.0/

謝謝!

+0

@wallydrag - 此答案有幫助嗎? – RockStar

相關問題