From a3abb6345887358a420cc68b774938de51ebf36c Mon Sep 17 00:00:00 2001 From: Philip Mueller Date: Mon, 30 Oct 2023 10:42:18 -0400 Subject: [PATCH] Nested failures fail, not raise --- lib/interactor.rb | 3 ++- spec/integration_spec.rb | 24 ++++++++++++++++++++++++ spec/support/lint.rb | 8 -------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/interactor.rb b/lib/interactor.rb index 307234c..146e064 100644 --- a/lib/interactor.rb +++ b/lib/interactor.rb @@ -115,8 +115,9 @@ def run run! rescue Failure => e if context.object_id != e.context.object_id - raise + @context = e.context end + @context.failure = true end # Internal: Invoke an Interactor instance along with all defined hooks. The diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index be6217f..cb0c24b 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -929,6 +929,30 @@ def rollback end end + context "when a nested call! fails in an interactor" do + let(:interactor3) { + build_interactor do + class NestedInteractor + include Interactor + + def call + context.fail!(something: :went_wrong) + end + end + + def call + NestedInteractor.call! + end + end + } + + it 'sets the failure status on enclosing interactor' do + result = interactor3.call + + expect(result.failure?).to eq(true) + end + end + context "when a nested call errors" do let(:interactor3) { build_interactor do diff --git a/spec/support/lint.rb b/spec/support/lint.rb index 55b84d5..eb5fd85 100644 --- a/spec/support/lint.rb +++ b/spec/support/lint.rb @@ -78,14 +78,6 @@ }.not_to raise_error end - it "raises other failures" do - expect(instance).to receive(:run!).and_raise(Interactor::Failure.new(Interactor::Context.new)) - - expect { - instance.run - }.to raise_error(Interactor::Failure) - end - it "raises other errors" do expect(instance).to receive(:run!).and_raise("foo")