Rails (3.2) アプリケーションを外部 Web サービス (キャンペーン モニター) に接続しています。この Web サービスでは、クライアントをセットアップするためにシステムを数回呼び出す必要があります。
各アクションを個別のレスキュー ブロックに入れ、トランザクションにラップしましたが、あまり見栄えがよくありません。各エラーの後にエスケープし、正しいメッセージを表示する必要があります。現在、それはただ逃げるだけです。
アクションごとに考えられるエラーのリストがあります。これらを読み取り、フラッシュ メッセージとしてフォーマットするにはどうすればよいですか?
たとえば、次のエラーです。
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
応答からエラー コードを抽出し、フラッシュ メッセージとして表示する最良の方法は何ですか?