Skip to content

Commit

Permalink
Change error aggregator to stop accepting param argument
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Apr 15, 2020
1 parent 828b3c5 commit 4606862
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
5 changes: 1 addition & 4 deletions lib/tty/option/error_aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ def initialize(errors = {}, **config)
#
# @param [TTY::Option::Error] error
# @param [String] message
# @param [TTY::Option::Parameter] param
#
# @api public
def call(error, message, param = nil)
def call(error, message)
is_class = error.is_a?(Class)

if @raise_if_missing
Expand All @@ -36,8 +35,6 @@ def call(error, message, param = nil)

if error.respond_to?(:param) && error.param
(@errors[error.param.name] ||= {}).merge!(type_key => message)
elsif param
(@errors[param.name] ||= {}).merge!(type_key => message)
else
(@errors[:messages] ||= []) << { type_key => message }
end
Expand Down
24 changes: 12 additions & 12 deletions spec/unit/error_aggregator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
}.to raise_error(TTY::Option::MissingParameter, "boom")
end

it "collects errors as class" do
it "collects errors as classes with custom messages" do
aggregator = described_class.new(raise_if_missing: false)
foo_param = TTY::Option::Parameter::Option.create(:foo)
bar_param = TTY::Option::Parameter::Option.create(:bar)

aggregator.(TTY::Option::MissingParameter, "foo boom", foo_param)
aggregator.(TTY::Option::MissingParameter, "bar boom", bar_param)
aggregator.(TTY::Option::MissingParameter, "foo boom")
aggregator.(TTY::Option::MissingParameter, "bar boom")

expect(aggregator.errors).to eq({
foo: {missing_parameter: "foo boom"},
bar: {missing_parameter: "bar boom"}
messages: [
{missing_parameter: "foo boom"},
{missing_parameter: "bar boom"}
]
})
end

it "collects errors as instances" do
it "collects errors as instances with custom messages" do
aggregator = described_class.new(raise_if_missing: false)
foo_param = TTY::Option::Parameter::Option.create(:foo)
bar_param = TTY::Option::Parameter::Option.create(:bar)

foo_error = TTY::Option::MissingParameter.new("foo boom")
bar_error = TTY::Option::MissingParameter.new("bar boom")
foo_error = TTY::Option::MissingParameter.new(foo_param)
bar_error = TTY::Option::MissingParameter.new(bar_param)

aggregator.(foo_error, foo_error.message, foo_param)
aggregator.(bar_error, bar_error.message, bar_param)
aggregator.(foo_error, "foo boom")
aggregator.(bar_error, "bar boom")

expect(aggregator.errors).to eq({
foo: {missing_parameter: "foo boom"},
Expand Down

0 comments on commit 4606862

Please sign in to comment.