From 1f1992c3e2509853543ce02e24697b687e812eba Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 1 Oct 2024 10:38:14 -0600 Subject: [PATCH 1/2] Add template information to multiple template error messages. In service of helping debug https://github.com/ViewComponent/view_component/issues/2114, this change fleshes out the template error messages to include the current list of templates. --- docs/CHANGELOG.md | 4 ++++ lib/view_component/compiler.rb | 2 +- lib/view_component/errors.rb | 12 ++++++++++-- test/sandbox/test/rendering_test.rb | 5 +++++ 4 files changed, 20 insertions(+), 3 deletions(-) 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..fec63f649 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:" + 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 " \ From 2bb09fc74bd02096e1d90960249ab60d62b3912a Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 1 Oct 2024 10:48:02 -0600 Subject: [PATCH 2/2] add newline --- lib/view_component/errors.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/view_component/errors.rb b/lib/view_component/errors.rb index fec63f649..5b59f6db9 100644 --- a/lib/view_component/errors.rb +++ b/lib/view_component/errors.rb @@ -22,7 +22,7 @@ def initialize(errors, templates = nil) if templates message << "\n" - message << "Templates:" + message << "Templates:\n" message << templates.map(&:inspect).join("\n") end