Cleanly fallback when failing to tokenize ERB templates
Fix: https://github.com/rails/rails/issues/48319 Followup: https://github.com/rails/rails/pull/48184
This commit is contained in:
parent
d026af43c3
commit
a2f685c7c0
@ -54,7 +54,7 @@ def translate_location(spot, backtrace_location, source)
|
|||||||
spot[:script_lines] = source.lines
|
spot[:script_lines] = source.lines
|
||||||
|
|
||||||
spot
|
spot
|
||||||
rescue LocationParsingError
|
rescue NotImplementedError, LocationParsingError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
8
actionview/test/fixtures/test/unparseable_runtime_error_2.html.erb
vendored
Normal file
8
actionview/test/fixtures/test/unparseable_runtime_error_2.html.erb
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<h1>Oh no!</h1>
|
||||||
|
This template has a runtime error
|
||||||
|
<b>
|
||||||
|
<% if true %>
|
||||||
|
<%= method_that_does_not_exist %>a
|
||||||
|
<% end %>
|
||||||
|
</b>
|
||||||
|
Yikes!
|
@ -234,6 +234,19 @@ def test_render_location_conditional_append
|
|||||||
|
|
||||||
assert_equal 8, translated_spot[:first_column]
|
assert_equal 8, translated_spot[:first_column]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_location_conditional_append_2
|
||||||
|
ex = assert_raises(ActionView::Template::Error) {
|
||||||
|
@view.render(template: "test/unparseable_runtime_error_2")
|
||||||
|
}
|
||||||
|
erb_btl = ex.backtrace_locations.first
|
||||||
|
|
||||||
|
# Get the spot information from ErrorHighlight
|
||||||
|
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
|
||||||
|
translated_spot = translating_frame.spot(ex.cause)
|
||||||
|
|
||||||
|
assert_instance_of Integer, translated_spot[:first_column]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_partial
|
def test_render_partial
|
||||||
|
@ -166,6 +166,7 @@ def self.tokenize(source) # :nodoc:
|
|||||||
while !source.eos?
|
while !source.eos?
|
||||||
pos = source.pos
|
pos = source.pos
|
||||||
source.scan_until(/(?:#{start_re}|#{finish_re})/)
|
source.scan_until(/(?:#{start_re}|#{finish_re})/)
|
||||||
|
raise NotImplementedError if source.matched.nil?
|
||||||
len = source.pos - source.matched.bytesize - pos
|
len = source.pos - source.matched.bytesize - pos
|
||||||
|
|
||||||
case source.matched
|
case source.matched
|
||||||
@ -176,13 +177,13 @@ def self.tokenize(source) # :nodoc:
|
|||||||
tokens << [:CODE, source.matched] unless source.matched.empty?
|
tokens << [:CODE, source.matched] unless source.matched.empty?
|
||||||
tokens << [:CLOSE, source.scan(finish_re)] unless source.eos?
|
tokens << [:CLOSE, source.scan(finish_re)] unless source.eos?
|
||||||
else
|
else
|
||||||
raise NotImplemented
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
when finish_re
|
when finish_re
|
||||||
tokens << [:CODE, source.string[pos, len]] if len > 0
|
tokens << [:CODE, source.string[pos, len]] if len > 0
|
||||||
tokens << [:CLOSE, source.matched]
|
tokens << [:CLOSE, source.matched]
|
||||||
else
|
else
|
||||||
raise NotImplemented, source.matched
|
raise NotImplementedError, source.matched
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user