From 6763012ee94bb2a40463e09d957ffd6b8609aac4 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 22 Jan 2020 10:44:28 +1100 Subject: [PATCH] Rescue unknown checkout errors --- app/controllers/checkout_controller.rb | 4 ++++ config/locales/en.yml | 1 + spec/controllers/checkout_controller_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 3b3b7f95c8..79a9e42310 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -68,6 +68,10 @@ class CheckoutController < Spree::CheckoutController render json: { path: order_path(@order) }, status: :ok end end + rescue StandardError => error + Bugsnag.notify(error) + flash[:error] = I18n.t("checkout.failed") + update_failed end # Clears the cached order. Required for #current_order to return a new order diff --git a/config/locales/en.yml b/config/locales/en.yml index 611ec91409..8a05a09673 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1181,6 +1181,7 @@ en: already_ordered: cart: "cart" message_html: "You have an order for this order cycle already. Check the %{cart} to see the items you ordered before. You can also cancel items as long as the order cycle is open." + failed: "The checkout failed. Please let us know so that we can process your order." shops: hubs: show_closed_shops: "Show closed shops" diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index 7c149f6f3b..a2ac420a2e 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -200,6 +200,14 @@ describe CheckoutController, type: :controller do expect(response.body).to eq({ path: spree.order_path(order) }.to_json) end + it "returns an error on unexpected failure" do + allow(order).to receive(:update_attributes).and_raise + + spree_post :update, format: :json, order: {} + expect(response.status).to eq(400) + expect(response.body).to eq({ errors: {}, flash: {error: I18n.t("checkout.failed")} }.to_json) + end + describe "stale object handling" do it "retries when a stale object error is encountered" do allow(ResetOrderService).to receive(:new).with(controller, order) { reset_order_service }