From 1e1e88fe51aa3f7de144ee7f135a0e97b5cca8a0 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 21 Jun 2019 16:05:54 +1000 Subject: [PATCH] Rescue checkout on shipping id conflict Helps to investigate https://github.com/openfoodfoundation/openfoodnetwork/issues/3924 A better solution would be to move the update of shipments out of the after_save callback and deal with it in controllers. Unfortunately, that's a big and tricky task. Since this exception is causing a lot of pain for some Australian farmers, I introduced more logging here to understand the problem better. The issue was observed in OFN v1 and may disappear with v2. But we don't know that and should monitor it. --- app/models/spree/order_decorator.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index fec601be76..a723ce1198 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -166,7 +166,21 @@ Spree::Order.class_eval do shipments.each do |shipment| next if shipment.shipped? update_adjustment! shipment.adjustment if shipment.adjustment - shipment.save # updates included tax + begin + shipment.save # updates included tax + rescue ActiveRecord::RecordNotUnique => error + # This error was seen in production on `shipment.save` above. + # It caused lost payments and duplicate payments due to database rollbacks. + # While we don't understand the cause of this error yet, we rescue here + # because an outdated shipping fee is not as bad as a lost payment. + # And the shipping fee is already up-to-date when this error occurs. + # https://github.com/openfoodfoundation/openfoodnetwork/issues/3924 + Bugsnag.notify(error) do |report| + report.add_tab(:order, attributes) + report.add_tab(:shipment, shipment.attributes) + report.add_tab(:shipment_in_db, Spree::Shipment.find_by_id(shipment.id).attributes) + end + end end end