Skip to content

Commit

Permalink
do not raise a runtime exception when a choice state does not match
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed May 22, 2024
1 parent 4ccb502 commit a0d0c26
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/floe/workflow/choice_rule/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def true?(context, input)
lhs = variable_value(context, input)
rhs = compare_value(context, input)

validate!(lhs)
return unless valid?(lhs)

case compare_key
when "IsNull" then is_null?(lhs)
Expand Down Expand Up @@ -58,8 +58,8 @@ def true?(context, input)

private

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

def is_null?(value) # rubocop:disable Naming/PredicateName
Expand Down
2 changes: 1 addition & 1 deletion lib/floe/workflow/states/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def finish
output = output_path.value(context, input)

context.next_state = next_state
context.output = output
context.output = next_state.nil? ? {"Error" => "States.NoChoiceMatched"} : output
super
end

Expand Down
2 changes: 1 addition & 1 deletion spec/workflow/choice_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
let(:input) { {} }

it "raises an exception" do
expect { subject }.to raise_exception(RuntimeError, "No such variable [$.foo]")
expect(subject).to be_nil
end
end

Expand Down
19 changes: 15 additions & 4 deletions spec/workflow/states/choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
let(:input) { {} }
let(:ctx) { Floe::Workflow::Context.new(:input => input) }
let(:state) { workflow.current_state }
let(:use_default) { true}
let(:workflow) do
make_workflow(
ctx, {
Expand All @@ -19,7 +20,7 @@
"Next" => "SecondMatchState"
},
],
"Default" => "DefaultState"
"Default" => (use_default ? "DefaultState" : nil)
},
"FirstMatchState" => {"Type" => "Succeed"},
"SecondMatchState" => {"Type" => "Succeed"},
Expand Down Expand Up @@ -53,9 +54,19 @@
end

describe "#run_nonblock!" do
context "with a missing variable" do
it "raises an exception" do
expect { state.run_nonblock! }.to raise_error(RuntimeError, "No such variable [$.foo]")
context "with a missing variable and no default" do
let(:use_default) { false }
it "returns an error" do
state.run_nonblock!
expect(ctx.next_state).to be_nil
expect(ctx.output).to eq({"Error" => "States.NoChoiceMatched"})
end
end

context "with a missing variable and a default" do
it "uses the default" do
state.run_nonblock!
expect(ctx.next_state).to eq("DefaultState")
end
end

Expand Down

0 comments on commit a0d0c26

Please sign in to comment.