Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template information to multiple template error messages. #2115

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Add template information to multiple template error messages.

*Joel Hawksley*

* Add `ostruct` to gemspec file to suppress stdlib removal warning.

*Jonathan Underwood*
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def gather_template_errors(raise_errors)
errors << "Colliding templates #{variant_names.sort.map { |v| "'#{v}'" }.to_sentence} found in #{@component}."
end

raise TemplateError.new(errors) if errors.any? && raise_errors
raise TemplateError.new(errors, @templates) if errors.any? && raise_errors

errors
end
Expand Down
12 changes: 10 additions & 2 deletions lib/view_component/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ def initialize(klass_name)
end

class TemplateError < StandardError
def initialize(errors)
super(errors.join("\n"))
def initialize(errors, templates = nil)
message = errors.join("\n")

if templates
message << "\n"
message << "Templates:\n"
message << templates.map(&:inspect).join("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want inspect output, or just the template name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the inspect output, as it shows what type of templates there are for the component and where they are defined:

Template file and inline render method found for variant 'phone' in VariantTemplateAndInlineVariantTemplateComponent. There can only be a template file or inline render method per variant.

Templates:

#<ViewComponent::Template:0x000000012a9980d8 @component=VariantTemplateAndInlineVariantTemplateComponent, @type=:file, @this_format=:html, @variant=:phone, @lineno=0, @path=\"/Users/joelhawksley/github/view_component/test/sandbox/app/components/variant_template_and_inline_variant_template_component.html+phone.erb\", @extension=\"erb\", @source=nil, @method_name=nil, @defined_on_self=true, @source_originally_nil=true, @call_method_name=\"call_phone\">

#<ViewComponent::Template:0x000000012a997ef8 @component=VariantTemplateAndInlineVariantTemplateComponent, @type=:inline_call, @this_format=:html, @variant=:phone, @lineno=nil, @path=nil, @extension=nil, @source=nil, @method_name=:call_phone, @defined_on_self=true, @source_originally_nil=true, @call_method_name=:call_phone>

end

super(message)
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ def test_raise_error_when_variant_template_file_and_inline_variant_call_exist
end
end

assert_includes(
error.message,
"ViewComponent::Template:"
)

assert_includes(
error.message,
"Template file and inline render method found for variant 'phone' in " \
Expand Down
Loading