2012-06-22 31 views
0

我將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消息的最佳方式是什麼?

回答

1

戴維說,如果異常是你要做的,你可以只捕獲異常並以該字符串的形式處理該異常。

例如:

begin 
    ... 
rescue Exception => ex 
    # here "ex.message" contains your string, you can do anything you want with it 
    # or parse as is: 
    flash[:notice] = ex.message 
end 

redirect_to edit_user_registration_path 

UPDATE

如果你結合自己與那些由Dave提出建議的解決方案,你會得到這樣的事情,與您可以使用自己的錯誤字符串:

begin 
    ... 
rescue Exception => ex 
    code = ex.message.match(/.*?- (\d+): (.*)/)[1] 

    case code 
    when '172' 
    flash[:notice] = 'Please wait 30 minutes to create more clients' 
    end 
end 

redirect_to edit_user_registration_path 
+0

謝謝,會看看 – simonmorley

+0

是否有另一種方法來獲得例外?目前,這給了我#的「未定義方法匹配」 – simonmorley

+0

對不起,您需要在'ex.message'而不是'ex'上調用'match'。更新了我的答案。 – JeanMertz

2

所有你能做的就是不同之處工作給予,如果沒有數據有效載荷可以嘗試解析消息字符串,用它原來的樣子,它的全部或部分映射到自己的消息,等

例如,如果您展示的例子消息是標準化的,正則表達式可以用來搶的數字和信息部:

[1] pry(main)> s = "CreateSend::BadRequest: The CreateSend API responded with the following error - 172: You can create a maximum of five (5) clients per thirty (30) minutes" 
=> "CreateSend::BadRequest: The CreateSend API responded with the following error - 172: You can create a maximum of five (5) clients per thirty (30) minutes" 
[2] pry(main)> md = s.match /.*?- (\d+): (.*)/ 
=> #<MatchData 
"CreateSend::BadRequest: The CreateSend API responded with the following error - 172: You can create a maximum of five (5) clients per thirty (30) minutes" 
1:"172" 
2:"You can create a maximum of five (5) clients per thirty (30) minutes"> 
[3] pry(main)> md[1] 
=> "172" 
[4] pry(main)> md[2] 
=> "You can create a maximum of five (5) clients per thirty (30) minutes" 
+0

嗨戴夫。任何你可以指引我走向一個例子的機會。我不知道如何獲得這些信息格式化等。S – simonmorley

+0

@simonmorley更新回答,以包含使用正則表達式的示例。如果從例外返回的所有消息都採用相同(或類似)格式,則此解決方案可能足夠。 –

+0

非常感謝,讓我現在玩:) – simonmorley

0

在這種特定情況下處理錯誤的最佳方法是通過專門處理錯誤圖書館自己定義。 createsend-ruby定義了CreateSendError,BadRequestUnauthorized。你不需要解析任何字符串。

下面是一個例子從README採取處理錯誤的,:

require 'createsend' 

CreateSend.api_key 'your_api_key' 

begin 
    cl = CreateSend::Client.new "4a397ccaaa55eb4e6aa1221e1e2d7122" 
    id = CreateSend::Campaign.create cl.client_id, "", "", "", "", "", "", "", [], [] 
    puts "New campaign ID: #{id}" 
    rescue CreateSend::BadRequest => br 
    puts "Bad request error: #{br}" 
    puts "Error Code: #{br.data.Code}" 
    puts "Error Message: #{br.data.Message}" 
    rescue Exception => e 
    puts "Error: #{e}" 
end 

data字段總是包含一個CodeMessage,其對應於status codes API documentation