Skip to content

Commit

Permalink
Fail actor play when an Interactor fails 🙅
Browse files Browse the repository at this point in the history
Closes #163
  • Loading branch information
sunny committed Jul 15, 2024
1 parent fc4cefe commit 1869678
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/service_actor/playable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def play_method(actor)
end

def play_interactor(actor)
result.merge!(actor.call(result.to_h).to_h)
interactor = actor.call(result.to_h)
result.merge!(interactor.to_h)
fail! if interactor.failure?
result
end
end
end
9 changes: 9 additions & 0 deletions spec/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,15 @@
end
end

context "when playing a failing interactor" do
it "fails" do
actor = PlayInteractorFailure.result(value: 5)
expect(actor).to be_a_failure
expect(actor.error).to eq("Failed with Interactor")
expect(actor.value).to eq(5 + 1)
end
end

context "when using advanced mode with checks and not adding message key" do
context "when using inclusion check" do
let(:expected_alert) do
Expand Down
11 changes: 11 additions & 0 deletions spec/examples/fail_with_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "interactor"

class FailWithInteractor
include Interactor

def call
context.fail!(error: "Failed with Interactor")
end
end
10 changes: 10 additions & 0 deletions spec/examples/play_interactor_failure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class PlayInteractorFailure < Actor
input :value, default: 1
output :value

play IncrementValueWithInteractor,
FailWithInteractor,
IncrementValueWithInteractor
end

0 comments on commit 1869678

Please sign in to comment.