我將Rails(3.2)應用程序連接到外部Web服務(活動監視器),這需要對其系統進行幾次調用才能設置客戶端。處理來自失敗的API調用的自定義異常代碼
我已經把每個行動放到單獨的救援塊中幷包裹在一個交易中,但事情看起來並不怎麼樣。我需要它在每個錯誤之後逃脫並顯示正確的消息。目前它只是逃脫。
我有每個操作可能出現的錯誤列表 - 如何讀取這些內容並將其格式化爲閃存信息?
例如,以下錯誤:
CreateSend::BadRequest: The CreateSend API responded with the following error - 172: You can create a maximum of five (5) clients per thirty (30) minutes
我現在有這個在我的控制器:
def create_send_setup
...
begin
client = CreateSend::Client.create(@user.company_name, @user.username, @user.email, "(GMT) Dublin, Edinburgh, Lisbon, London", @user.country)
rescue
flash[:notice] = "Uh oh, there's been a problem. Please try again. Client"
end
begin
@client = CreateSend::Client.new(@user.createsend_client_api)
@client.set_access(@user.username, @password, @user.createsend_access_level)
@client.set_monthly_billing(@user.createsend_currency, true, @user.createsend_markup)
rescue
flash[:notice] = "Uh oh, there's been a problem. Please try again. Access"
end
flash[:notice] = "Great, now you just need to choose a plan"
redirect_to edit_user_registration_path
end
在前面的積分,我用類似
case bla.code.to_s
when /2../
when '403'
raise "Could not update subscriber: new-customer-id is already in use."
...
end
從響應中提取錯誤代碼並顯示爲flash消息的最佳方式是什麼?
謝謝,會看看 – simonmorley
是否有另一種方法來獲得例外?目前,這給了我#的「未定義方法匹配」 –
simonmorley
對不起,您需要在'ex.message'而不是'ex'上調用'match'。更新了我的答案。 – JeanMertz