Skip to content

Commit

Permalink
Fail actor when playing a failing Interactor 🙅 (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny authored Jul 15, 2024
1 parent fc4cefe commit 9f299c1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## unreleased

Fixes:
- Fail actor when playing a failing Interactor (#164)

## v3.9.2

Fixes:
Expand Down
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 9f299c1

Please sign in to comment.