我有以下代码体,从理论上讲,它可以挽救嵌套条件块内部的错误。然而;它不起作用。我尝试将急救措施和begin语句移到两个块中,但都无法正常工作。
begin
@acceptrecent = Accept.where("buyer_id = ? OR localbid = ?", current_user, current_user).order(created_at: :desc).limit(5)
@offersrecent = Offer.where(buyer_id:current_user, offerstat: true).order(updated_at: :desc).limit(20)
@buyer = Buyer.find(current_user)
@acceptrecent.each do |item|
unless item.processed.present?
item.cart.nil? ? tracker = TrackDefault.new : tracker = USPS::Request::TrackingFieldLookup.new(item.cart.track_number,1).send!
item.status = tracker.status
item.save
end
end
rescue USPS::Error => err
tracker = TrackDefault.new
end
答案 0 :(得分:0)
问题在于,当引发异常时,执行将跳至救援部分,因此将跳过所有必需的代码。尝试将begin...rescue...end
块放在循环中:
@acceptrecent = Accept.where("buyer_id = ? OR localbid = ?", current_user, current_user).order(created_at: :desc).limit(5)
@offersrecent = Offer.where(buyer_id:current_user, offerstat: true).order(updated_at: :desc).limit(20)
@buyer = Buyer.find(current_user)
@acceptrecent.each do |item|
unless item.processed.present?
tracker =
begin
item.cart ? USPS::Request::TrackingFieldLookup.new(item.cart.track_number,1).send! : TrackDefault.new
rescue USPS::Error => _
TrackDefault.new
end
item.status = tracker.status
item.save
end
end