我正在将我们的 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 消息的最佳方法是什么?