1

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

応答からエラー コードを抽出し、フラッシュ メッセージとして表示する最良の方法は何ですか?

4

3 に答える 3

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"
于 2012-06-22T12:54:25.410 に答える
1

Daveが言ったように、例外がすべてである場合は、例外をキャプチャして、その例外を文字列として使用することしかできません。

例えば:

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

アップデート

私の提案したソリューションを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
于 2012-06-22T13:42:48.777 に答える
1

この特定の状況でエラーを処理する最善の方法は、ライブラリ自体が定義するエラーを具体的に処理することです。createsend-ruby は、 CreateSendErrorBadRequest、およびUnauthorizedを定義します。文字列を解析する必要はありません。

エラー処理の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

このフィールドには、ステータス コードの API ドキュメントに対応するとdataが常に含まれます。CodeMessage

于 2012-10-19T07:26:13.940 に答える