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