Skip to content

Commit 728a2d5

Browse files
authored
Merge pull request rails#54410 from Edouard-chin/ec-syntax-error
No dump of the entire template when a syntax error is encountered:
2 parents 6d63b15 + 87296b4 commit 728a2d5

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

actionview/lib/action_view/template/error.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,13 @@ def initialize(template, offending_code_string)
260260
end
261261

262262
def message
263-
<<~MESSAGE
264-
Encountered a syntax error while rendering template: check #{@offending_code_string}
265-
MESSAGE
263+
if template.is_a?(Template::Inline)
264+
<<~MESSAGE
265+
Encountered a syntax error while rendering template: check #{@offending_code_string}
266+
MESSAGE
267+
else
268+
"Encountered a syntax error while rendering template located at: #{template.short_identifier}"
269+
end
266270
end
267271

268272
def annotated_source_code

railties/test/application/rendering_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,69 @@ def self.call(_, source)
8686
assert_equal 200, last_response.status
8787
assert_equal({ format: :awesome, handler: RubbyHandler }.inspect, last_response.body)
8888
end
89+
90+
test "template content is dumped if rendered inline and a syntax error is encountered" do
91+
app_file "config/routes.rb", <<-RUBY
92+
Rails.application.routes.draw do
93+
root to: 'pages#show'
94+
end
95+
RUBY
96+
97+
app_file "app/controllers/pages_controller.rb", <<-RUBY
98+
class PagesController < ApplicationController
99+
layout false
100+
101+
def show
102+
render(inline: "<% [ %>")
103+
end
104+
end
105+
RUBY
106+
107+
app("development")
108+
109+
get("/", {}, { "HTTPS" => "on" })
110+
111+
assert_equal 500, last_response.status
112+
document = Nokogiri::HTML5.parse(last_response.body)
113+
nodes = document.css("div.exception-message>div.message")
114+
115+
assert_not_empty(nodes)
116+
assert_equal("Encountered a syntax error while rendering template: check <% [ %>\n", nodes.first.text)
117+
end
118+
119+
test "template content is not dumped when rendered from file and a syntax error is encountered" do
120+
app_file "config/routes.rb", <<-RUBY
121+
Rails.application.routes.draw do
122+
root to: 'pages#show'
123+
end
124+
RUBY
125+
126+
app_file "app/controllers/pages_controller.rb", <<-RUBY
127+
class PagesController < ApplicationController
128+
layout false
129+
130+
def show
131+
end
132+
end
133+
RUBY
134+
135+
app_file "app/views/pages/show.html.erb", <<-RUBY
136+
<% [ %>
137+
RUBY
138+
139+
app("development")
140+
141+
get("/", {}, { "HTTPS" => "on" })
142+
143+
assert_equal 500, last_response.status
144+
document = Nokogiri::HTML5.parse(last_response.body)
145+
nodes = document.css("div.exception-message>div.message")
146+
147+
assert_not_empty(nodes)
148+
assert_equal(
149+
"Encountered a syntax error while rendering template located at: app/views/pages/show.html.erb",
150+
nodes.first.text,
151+
)
152+
end
89153
end
90154
end

0 commit comments

Comments
 (0)