如何在 Rails 上使用 Ruby 处理用于导入文本文件的事务中的多个异常
How to handle multiple exceptions in a Transaction with Ruby on Rails for importing a text file
我的 ruby 环境是:Ruby 2.3.1
和 Rails 5.0.0.1
。
我正在尝试导入一个文本文件以导入大量购买项目。
采购文件示例:
data.txt
Customer\tDescription\tUnit Price\tQuantity\tAddress\tSupply company\n
Athos Matthew\tChocolate\t10\t5\tSome address.\tChocolate company\n
各列由一个制表符 (\t) 分隔,最后一个 (\n) 有一个回车符。
我购买了 class,其中所有属性都不能为 null。属性是:
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 class 来完成这项工作并使代码更简洁。
我的问题是交易部分:
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 classes,但使用我的代码,我只能捕获产品生成的第一个异常错误。如果 SupplyCompany 或 Purchase 发生错误,我会丢失这些错误消息。
导入文件时是否有其他导入和记录错误消息的方法?
由于您正在救援 Exception
,因此很难知道实际出现的错误是什么。救援时,您应该尽可能使用更具体的class。
您也可能根本不需要使用救援。您正在使用的活动方法:find_or_create_by!
和 save!
可以在没有感叹号的情况下编写,这样它们就不会引发错误。
在活动记录中,如果您尝试保存验证错误的内容,则会填充 <record>.errors.full_messages
数组。如果您不使用感叹号,它不一定会引发错误(尽管无论如何都可能引发错误)。
因此,例如,您可以尝试保存记录并检查错误,如下所示:
product = Product.find_or_initialize_by(description: data[1], price: data[2])
product.save
errors[:import][index] ||= []
errors[:import][index].concat product.errors_full_messages
其实在这种情况下我觉得你的做法是有一定道理的。您正在按顺序保存一些记录。如果第一个失败,那么其他人可能也会失败 - 那么是否值得尝试保存这些后续记录?我会让你决定。
你可以有更具体的异常处理...为你想捕获的每个部分做一个救援,最后如果遇到任何以前的错误则引发错误(让你离开事务块)并测试在最后一个错误中,您正在拯救自己的 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 环境是:Ruby 2.3.1
和 Rails 5.0.0.1
。
我正在尝试导入一个文本文件以导入大量购买项目。
采购文件示例:
data.txt
Customer\tDescription\tUnit Price\tQuantity\tAddress\tSupply company\n Athos Matthew\tChocolate\t10\t5\tSome address.\tChocolate company\n
各列由一个制表符 (\t) 分隔,最后一个 (\n) 有一个回车符。
我购买了 class,其中所有属性都不能为 null。属性是:
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 class 来完成这项工作并使代码更简洁。
我的问题是交易部分:
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 classes,但使用我的代码,我只能捕获产品生成的第一个异常错误。如果 SupplyCompany 或 Purchase 发生错误,我会丢失这些错误消息。
导入文件时是否有其他导入和记录错误消息的方法?
由于您正在救援 Exception
,因此很难知道实际出现的错误是什么。救援时,您应该尽可能使用更具体的class。
您也可能根本不需要使用救援。您正在使用的活动方法:find_or_create_by!
和 save!
可以在没有感叹号的情况下编写,这样它们就不会引发错误。
在活动记录中,如果您尝试保存验证错误的内容,则会填充 <record>.errors.full_messages
数组。如果您不使用感叹号,它不一定会引发错误(尽管无论如何都可能引发错误)。
因此,例如,您可以尝试保存记录并检查错误,如下所示:
product = Product.find_or_initialize_by(description: data[1], price: data[2])
product.save
errors[:import][index] ||= []
errors[:import][index].concat product.errors_full_messages
其实在这种情况下我觉得你的做法是有一定道理的。您正在按顺序保存一些记录。如果第一个失败,那么其他人可能也会失败 - 那么是否值得尝试保存这些后续记录?我会让你决定。
你可以有更具体的异常处理...为你想捕获的每个部分做一个救援,最后如果遇到任何以前的错误则引发错误(让你离开事务块)并测试在最后一个错误中,您正在拯救自己的 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