我的 ruby 环境是:Ruby 2.3.1
和 Rails 5.0.0.1
。
我正在尝试导入一个文本文件以导入大量购买项目。
购买文件示例:
数据.txt
Customer\tDescription\tUnit Price\tQuantity\tAddress\tSupply company\n Athos Matthew\tChocolate\t10\t5\tSome address.\tChocolate company\n
列由一个制表符 (\t) 分隔,最后一个 (\n) 有一个回车符。
我有一个购买类,其中所有属性都不能为空。属性是:
custumer_name:string
product_id:integer # It has relationship with the Product Resource
product_quantity:integer
supply_company_id:integer # It has relationship with the SupplyCompany Resource
为了导入文件,我决定创建一个 PurchaseImporter 类来完成这项工作并使代码更简洁。
我的问题是交易部分:
begin
ActiveRecord::Base.transaction do
purchase = Purchase.new
data = line.force_encoding('UTF-8').split(/\t/)
purchase.customer_name = data[0]
product = Product.find_or_create_by!(description: data[1], price: data[2])
purchase.product_quantity = data[3]
purchase.product = product
supply_company = SupplyCompany.find_or_create_by!(name: data[5], address: data[4])
purchase.supply_company = supply_company
purchase.save!
end
rescue Exception => e
@errors[:import][index] = e.message
end
我的问题是我想捕获所有可能在该交易中发生的 Product、SupplyCompany 和 Purchase 引发的错误。
这是没有不必要代码解释的事件顺序。
product = Product.find_or_create_by!(description: data[1], price: data[2])
supply_company = SupplyCompany.find_or_create_by!(name: data[5], address: data[4])
purchase.save!
我需要将此错误信息打印到屏幕中的这 3 个类,但使用我的代码,我只能捕获产品生成的第一个异常错误。如果 SupplyCompany 或 Purchase 发生错误,我将丢失这些错误消息。
导入文件时是否有其他方法导入并记录错误消息?
最佳答案
你可以有更具体的异常处理......为你想要捕获的每个部分做一个救援,最后如果遇到任何以前的错误(让你离开事务 block )并在其中进行测试最后一个错误,你正在拯救你自己的 raise
否则它是一些其他问题,你需要停止。
begin
ActiveRecord::Base.transaction do
error_encountered = false
purchase = Purchase.new
data = line.force_encoding('UTF-8').split(/\t/)
purchase.customer_name = data[0]
begin
product = Product.find_or_create_by!(description: data[1], price: data[2])
purchase.product_quantity = data[3]
purchase.product = product
rescue Exception => e
@errors[:import][index] = e.message
error_encountered = true
end
begin
supply_company = SupplyCompany.find_or_create_by!(name: data[5], address: data[4])
purchase.supply_company = supply_company
rescue Exception => e
@errors[:import][index] = e.message
error_encountered = true
end
begin
purchase.save!
rescue Exception => e
@errors[:import][index] = e.message
error_encountered = true
end
raise 'break out of transaction' if error_encountered
end
rescue Exception => e
raise unless e.message == 'break out of transaction'
end
关于ruby-on-rails - 如何使用 Ruby on Rails 处理事务中的多个异常以导入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468312/