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.
This commit is contained in:
Maikel Linke
2019-06-21 16:05:54 +10:00
parent e4c3c1664a
commit 1e1e88fe51

View File

@@ -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