Skip to content

Commit ec82091

Browse files
committed
do not raise a runtime exception when a choice state does not match
1 parent 5fa26cd commit ec82091

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

lib/floe/workflow/choice_rule/data.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def initialize(*)
1818
def true?(context, input)
1919
lhs = variable_value(context, input)
2020
rhs = compare_value(context, input)
21-
22-
validate!(lhs)
21+
return false unless valid?(lhs)
2322

2423
case compare_key
2524
when "IsNull" then is_null?(lhs)
@@ -58,8 +57,8 @@ def true?(context, input)
5857

5958
private
6059

61-
def validate!(value)
62-
raise "No such variable [#{variable}]" if value.nil? && !%w[IsNull IsPresent].include?(compare_key)
60+
def valid?(value)
61+
!value.nil? || %w[IsNull IsPresent].include?(compare_key)
6362
end
6463

6564
def is_null?(value) # rubocop:disable Naming/PredicateName

lib/floe/workflow/states/choice.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def finish
2323
next_state = choices.detect { |choice| choice.true?(context, output) }&.next || default
2424

2525
context.next_state = next_state
26-
context.output = output
26+
context.output = next_state.nil? ? {"Error" => "States.NoChoiceMatched"} : output
2727
super
2828
end
2929

spec/workflow/choice_rule_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
let(:input) { {} }
8080

8181
it "raises an exception" do
82-
expect { subject }.to raise_exception(RuntimeError, "No such variable [$.foo]")
82+
expect(subject).to eq(false)
8383
end
8484
end
8585

spec/workflow/states/choice_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
let(:input) { {} }
33
let(:ctx) { Floe::Workflow::Context.new(:input => input) }
44
let(:state) { workflow.current_state }
5+
let(:use_default) { true }
56
let(:workflow) do
67
make_workflow(
78
ctx, {
@@ -19,7 +20,7 @@
1920
"Next" => "SecondMatchState"
2021
},
2122
],
22-
"Default" => "DefaultState"
23+
"Default" => (use_default ? "DefaultState" : nil)
2324
},
2425
"FirstMatchState" => {"Type" => "Succeed"},
2526
"SecondMatchState" => {"Type" => "Succeed"},
@@ -53,9 +54,19 @@
5354
end
5455

5556
describe "#run_nonblock!" do
56-
context "with a missing variable" do
57-
it "raises an exception" do
58-
expect { state.run_nonblock! }.to raise_error(RuntimeError, "No such variable [$.foo]")
57+
context "with a missing variable and no default" do
58+
let(:use_default) { false }
59+
it "returns an error" do
60+
state.run_nonblock!
61+
expect(ctx.next_state).to be_nil
62+
expect(ctx.output).to eq({"Error" => "States.NoChoiceMatched"})
63+
end
64+
end
65+
66+
context "with a missing variable and a default" do
67+
it "uses the default" do
68+
state.run_nonblock!
69+
expect(ctx.next_state).to eq("DefaultState")
5970
end
6071
end
6172

0 commit comments

Comments
 (0)