Skip to content

Commit

Permalink
Raise exception on invalid argument type to .call or .call!
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
taylorthurlow committed Nov 2, 2020
1 parent e52087d commit 6a2c122
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/interaktor/callable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def success(*attributes, **options)
#
# @return [Interaktor::Context] the context, following interaktor execution
def call(context = {})
unless context.is_a?(Hash) || context.is_a?(Interaktor::Context)
raise ArgumentError, "Expected a hash argument when calling the interaktor, got a #{context.class} instead."
end

apply_default_optional_attributes(context)
verify_attribute_presence(context)

Expand All @@ -161,6 +165,10 @@ def call(context = {})
#
# @return [Interaktor::Context] the context, following interaktor execution
def call!(context = {})
unless context.is_a?(Hash) || context.is_a?(Interaktor::Context)
raise ArgumentError, "Expected a hash argument when calling the interaktor, got a #{context.class} instead."
end

apply_default_optional_attributes(context)
verify_attribute_presence(context)

Expand Down
12 changes: 12 additions & 0 deletions spec/support/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
interaktor.call(baz: "wadus")
}.to raise_error(an_instance_of(Interaktor::Error::UnknownAttributeError).and having_attributes(attributes: [:baz]))
end

it "fails when a non-hash or non-context argument is passed" do
expect {
interaktor.call("foo")
}.to raise_error(ArgumentError, /Expected a hash argument/)
end
end

describe ".call!" do
Expand All @@ -35,6 +41,12 @@
interaktor.call!(baz: "wadus")
}.to raise_error(an_instance_of(Interaktor::Error::UnknownAttributeError).and having_attributes(attributes: [:baz]))
end

it "fails when a non-hash or non-context argument is passed" do
expect {
interaktor.call!("foo")
}.to raise_error(ArgumentError, /Expected a hash argument/)
end
end

describe "#run" do
Expand Down

0 comments on commit 6a2c122

Please sign in to comment.