Skip to content

Commit

Permalink
Add template information to multiple template error messages. (#2115)
Browse files Browse the repository at this point in the history
* Add template information to multiple template error messages.

In service of helping debug #2114,
this change fleshes out the template error messages to include
the current list of templates.

* add newline
  • Loading branch information
joelhawksley authored Oct 1, 2024
1 parent 553d63f commit f87c87b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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")
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

0 comments on commit f87c87b

Please sign in to comment.