Skip to content

Commit

Permalink
feat: Improve variant not found error message (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Sep 29, 2024
1 parent 146d55d commit c4a9100
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/phlex/variants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ def build_variants_style(variants)
next value if value
end

raise VariantNotFoundError, "Variant `#{variant}: #{option.inspect}` doesn't exist"
raise_variant_not_found_error(options, variant, option)
end
end

def raise_variant_not_found_error(options, variant, option)
message = if options
"Option #{option.inspect} for #{variant.inspect} variant doesn't exist. Valid options are: #{options.keys}"
else
"Variant #{variant.inspect} doesn't exist. Available variants are: #{self::STYLE_VARIANTS.keys}"
end

raise VariantNotFoundError, message
end
end

private
Expand Down
14 changes: 11 additions & 3 deletions spec/phlex/variants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,33 @@ def view_template
expect(actual_html).to eq expected_html
end

it "raises error with non existent variants" do
it "raises error with non existent options" do
example = phlex_class do
style do
variants do
color do
primary "btn-primary"
danger "btn-danger"
end

size do
sm "btn-sm"
md "btn-md"
end
end
end
end

missing_option = "Option :warning for :color variant doesn't exist. Valid options are: [:primary, :danger]"
missing_variant = "Variant :disabled doesn't exist. Available variants are: [:color, :size]"

expect do
example.build_style(color: :warning)
end.to raise_error(Phlex::Variants::VariantNotFoundError, "Variant `color: :warning` doesn't exist")
end.to raise_error(Phlex::Variants::VariantNotFoundError, missing_option)

expect do
example.build_style(disabled: true)
end.to raise_error(Phlex::Variants::VariantNotFoundError, "Variant `disabled: true` doesn't exist")
end.to raise_error(Phlex::Variants::VariantNotFoundError, missing_variant)
end

it "allows defining boolean variants" do
Expand Down

0 comments on commit c4a9100

Please sign in to comment.