Skip to content

Commit 37cf61f

Browse files
authored
Clarify error message about render-dependent logic (#2217)
* Clarify error message about render-dependent logic * changelog
1 parent 78c09b2 commit 37cf61f

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ nav_order: 5
1414

1515
*Kane Jamison*
1616

17+
* Clarify error message about render-dependent logic.
18+
19+
Error messages about render-dependent logic were sometimes inaccurate, saying `during initialization` despite also being raised after a component had been initialized but before it was rendered.
20+
21+
*Joel Hawksley*
22+
1723
* Remove JS and CSS docs as they proved difficult to maintain and lacked consensus.
1824

1925
*Joel Hawksley*

lib/view_component/errors.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class NilWithContentError < BaseError
177177

178178
class TranslateCalledBeforeRenderError < BaseError
179179
MESSAGE =
180-
"`#translate` can't be used during initialization as it depends " \
180+
"`#translate` can't be used before rendering as it depends " \
181181
"on the view context that only exists once a ViewComponent is passed to " \
182182
"the Rails render pipeline.\n\n" \
183183
"It's sometimes possible to fix this issue by moving code dependent on " \
@@ -186,7 +186,7 @@ class TranslateCalledBeforeRenderError < BaseError
186186

187187
class HelpersCalledBeforeRenderError < BaseError
188188
MESSAGE =
189-
"`#helpers` can't be used during initialization as it depends " \
189+
"`#helpers` can't be used before rendering as it depends " \
190190
"on the view context that only exists once a ViewComponent is passed to " \
191191
"the Rails render pipeline.\n\n" \
192192
"It's sometimes possible to fix this issue by moving code dependent on " \
@@ -195,7 +195,7 @@ class HelpersCalledBeforeRenderError < BaseError
195195

196196
class ControllerCalledBeforeRenderError < BaseError
197197
MESSAGE =
198-
"`#controller` can't be used during initialization, as it depends " \
198+
"`#controller` can't be used before rendering, as it depends " \
199199
"on the view context that only exists once a ViewComponent is passed to " \
200200
"the Rails render pipeline.\n\n" \
201201
"It's sometimes possible to fix this issue by moving code dependent on " \

test/sandbox/test/base_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_calling_helpers_outside_render_raises
4343
assert_raises ViewComponent::HelpersCalledBeforeRenderError do
4444
component.helpers
4545
end
46-
assert_includes err.message, "can't be used during initialization"
46+
assert_includes err.message, "can't be used before rendering"
4747
end
4848

4949
def test_calling_controller_outside_render_raises
@@ -53,7 +53,7 @@ def test_calling_controller_outside_render_raises
5353
component.controller
5454
end
5555

56-
assert_includes err.message, "can't be used during initialization"
56+
assert_includes err.message, "can't be used before rendering"
5757
end
5858

5959
def test_sidecar_files

test/sandbox/test/rendering_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ def test_renders_component_with_initializer_translations
394394
assert_raises ViewComponent::TranslateCalledBeforeRenderError do
395395
render_inline(InitializerTranslationsComponent.new)
396396
end
397-
assert_includes err.message, "can't be used during initialization"
397+
assert_includes err.message, "can't be used before rendering"
398398
end
399399

400400
def test_renders_component_with_rb_in_its_name

0 commit comments

Comments
 (0)