From a0d5d7843464c646668e7250055a94008b6cb04f Mon Sep 17 00:00:00 2001 From: Alex Vondrak Date: Mon, 25 Sep 2023 15:21:10 -0700 Subject: [PATCH] Rescue from Exception, not just StandardError --- lib/ar_after_transaction.rb | 2 +- spec/ar_after_transaction_spec.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/ar_after_transaction.rb b/lib/ar_after_transaction.rb index bb3743a..21da49f 100644 --- a/lib/ar_after_transaction.rb +++ b/lib/ar_after_transaction.rb @@ -48,7 +48,7 @@ def transaction_with_after(**args) clean = false raise end - rescue StandardError + rescue Exception clean = false raise ensure diff --git a/spec/ar_after_transaction_spec.rb b/spec/ar_after_transaction_spec.rb index 72b5024..8c40aa0 100644 --- a/spec/ar_after_transaction_spec.rb +++ b/spec/ar_after_transaction_spec.rb @@ -34,6 +34,10 @@ def oops def raise_rollback raise ActiveRecord::Rollback end + + def sigterm + raise SignalException.new('TERM') + end end describe ARAfterTransaction do @@ -54,12 +58,18 @@ def raise_rollback expect(User.test_stack).to eq [:normal, :after] end - it 'does not execute when transaction was rolled back' do + it 'does not execute when transaction was rolled back by a StandardError' do User.test_callbacks = [:do_after, :do_normal, :oops] expect(-> { User.create! }).to raise_error(AnExpectedError) expect(User.test_stack).to eq [:normal] end + it 'does not execute when transaction was rolled back by an Exception' do + User.test_callbacks = [:do_after, :do_normal, :sigterm] + expect(-> { User.create! }).to raise_error(SignalException) + expect(User.test_stack).to eq [:normal] + end + it 'does not execute when transaction gets rolled back by ActiveRecord::Rollback '\ 'raised in an after_create callback' do User.test_callbacks = [:do_after, :do_normal, :raise_rollback]