Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail actor when playing a failing Interactor 🙅 #164

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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