Skip to content

Commit

Permalink
Report backorder errors instead of failing checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Sep 25, 2024
1 parent 25ea08a commit ef4c2d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/jobs/backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@ def self.check_stock(order)
end

perform_later(order, linked_variants) if linked_variants.present?
rescue StandardError => e
# Errors here shouldn't affect the checkout. So let's report them
# separately:
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
end
end

def perform(order, linked_variants)
OrderLocker.lock_order_and_variants(order) do
place_backorder(order, linked_variants)
end
rescue StandardError => e
# If the backordering fails, we need to tell the shop owner because they
# need to organgise more stock.
Bugsnag.notify(e) do |payload|
payload.add_metadata(:order, order)
payload.add_metadata(:linked_variants, linked_variants)
end
end

def place_backorder(order, linked_variants)
Expand Down
18 changes: 18 additions & 0 deletions spec/jobs/backorder_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
BackorderJob.check_stock(order)
}.to enqueue_job(BackorderJob).with(order, [variant])
end

it "reports errors" do
expect(Bugsnag).to receive(:notify).and_call_original

expect {
BackorderJob.check_stock(nil)
}.not_to raise_error
end
end

describe "#peform" do
it "reports errors" do
expect(Bugsnag).to receive(:notify).and_call_original

expect {
subject.perform(nil, [])
}.not_to raise_error
end
end

describe "#place_backorder" do
Expand Down

0 comments on commit ef4c2d1

Please sign in to comment.