From c4a9100984fcdd3dd8b618f51315357fc08a23b4 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 29 Sep 2024 11:07:42 -0300 Subject: [PATCH] feat: Improve variant not found error message (#7) --- lib/phlex/variants.rb | 12 +++++++++++- spec/phlex/variants_spec.rb | 14 +++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/phlex/variants.rb b/lib/phlex/variants.rb index 8169b71..cb32420 100644 --- a/lib/phlex/variants.rb +++ b/lib/phlex/variants.rb @@ -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 diff --git a/spec/phlex/variants_spec.rb b/spec/phlex/variants_spec.rb index 67f57cf..0b4d9c8 100644 --- a/spec/phlex/variants_spec.rb +++ b/spec/phlex/variants_spec.rb @@ -142,7 +142,7 @@ 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 @@ -150,17 +150,25 @@ def view_template 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