1

我正在将我们的 Rails (3.2) 应用程序连接到外部 Web 服务(活动监视器),这需要对其系统进行几次调用来设置客户端。

我已将每个操作放入单独的救援块中并包装在事务中,但事情看起来并不那么好。我需要它在每个错误后转义并显示正确的消息。目前它只是逃脱。

我有每个操作可能出现的错误列表 - 如何读取这些错误并将其格式化为 Flash 消息?

例如,以下错误:

 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 消息的最佳方法是什么?

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

正如戴夫所说,如果异常是你所拥有的,你只能捕获异常并将该异常作为字符串做一些事情。

例如:

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

在这种特定情况下处理错误的最佳方法是专门处理库自己定义的错误。created-ruby 定义了CreateSendErrorBadRequestUnauthorized。您不需要解析任何字符串。

以下是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,对应于状态代码 API 文档

于 2012-10-19T07:26:13.940 回答