diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2dfb62179..ba636c62b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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* diff --git a/lib/view_component/compiler.rb b/lib/view_component/compiler.rb index e29141a3e..8e6163acc 100644 --- a/lib/view_component/compiler.rb +++ b/lib/view_component/compiler.rb @@ -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 diff --git a/lib/view_component/errors.rb b/lib/view_component/errors.rb index 79eda652e..5b59f6db9 100644 --- a/lib/view_component/errors.rb +++ b/lib/view_component/errors.rb @@ -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 diff --git a/test/sandbox/test/rendering_test.rb b/test/sandbox/test/rendering_test.rb index afd4e1ca7..b06bcf118 100644 --- a/test/sandbox/test/rendering_test.rb +++ b/test/sandbox/test/rendering_test.rb @@ -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 " \