Skip to content

Commit

Permalink
Verify configuration exception classes in setter method
Browse files Browse the repository at this point in the history
  • Loading branch information
viralpraxis committed Feb 28, 2024
1 parent bf261d7 commit 8a0dd40
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/service_actor/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ def inherited(child)
child.failure_class = failure_class || ServiceActor::Failure
end

attr_accessor :argument_error_class, :failure_class
def argument_error_class=(value)
validate_provided_error_class(value)

@argument_error_class = value
end

def failure_class=(value)
validate_provided_error_class(value)

@failure_class = value
end

attr_reader :argument_error_class, :failure_class

private

def validate_provided_error_class(value)
return if value.is_a?(Class) && value <= Exception

raise ArgumentError, "Expected #{value} to be a subclass of Exception"
end
end
end
56 changes: 56 additions & 0 deletions spec/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,62 @@
end
end
end

context "with `failure_class` which is not a class" do
let(:actor) do
Class.new(Actor) do
self.failure_class = 1
end
end

it do
expect { actor }.to raise_error(
ArgumentError, "Expected 1 to be a subclass of Exception"
)
end
end

context "with `failure_class` that does not inherit `Exception`" do
let(:actor) do
Class.new(Actor) do
self.failure_class = Class.new
end
end

it do
expect { actor }.to raise_error(
ArgumentError, /Expected .+ to be a subclass of Exception/
)
end
end

context "with `argument_error_class` which is not a class" do
let(:actor) do
Class.new(Actor) do
self.argument_error_class = 1
end
end

it do
expect { actor }.to raise_error(
ArgumentError, "Expected 1 to be a subclass of Exception"
)
end
end

context "with `argument_error_class` that does not inherit `Exception`" do
let(:actor) do
Class.new(Actor) do
self.argument_error_class = Class.new
end
end

it do
expect { actor }.to raise_error(
ArgumentError, /Expected .+ to be a subclass of Exception/
)
end
end
end

describe "#result" do
Expand Down

0 comments on commit 8a0dd40

Please sign in to comment.