Skip to content

Commit

Permalink
Cleanly fallback when failing to tokenize ERB templates
Browse files Browse the repository at this point in the history
Fix: #48319
Followup: #48184
  • Loading branch information
byroot committed Jun 1, 2023
1 parent d026af4 commit a2f685c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion actionview/lib/action_view/template/handlers/erb.rb
Expand Up @@ -54,7 +54,7 @@ def translate_location(spot, backtrace_location, source)
spot[:script_lines] = source.lines

spot
rescue LocationParsingError
rescue NotImplementedError, LocationParsingError
nil
end

Expand Down
@@ -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!
13 changes: 13 additions & 0 deletions actionview/test/template/render_test.rb
Expand Up @@ -234,6 +234,19 @@ def test_render_location_conditional_append

assert_equal 8, translated_spot[:first_column]
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

def test_render_partial
Expand Down
5 changes: 3 additions & 2 deletions activesupport/lib/active_support/core_ext/erb/util.rb
Expand Up @@ -166,6 +166,7 @@ def self.tokenize(source) # :nodoc:
while !source.eos?
pos = source.pos
source.scan_until(/(?:#{start_re}|#{finish_re})/)
raise NotImplementedError if source.matched.nil?
len = source.pos - source.matched.bytesize - pos

case source.matched
Expand All @@ -176,13 +177,13 @@ def self.tokenize(source) # :nodoc:
tokens << [:CODE, source.matched] unless source.matched.empty?
tokens << [:CLOSE, source.scan(finish_re)] unless source.eos?
else
raise NotImplemented
raise NotImplementedError
end
when finish_re
tokens << [:CODE, source.string[pos, len]] if len > 0
tokens << [:CLOSE, source.matched]
else
raise NotImplemented, source.matched
raise NotImplementedError, source.matched
end
end

Expand Down

0 comments on commit a2f685c

Please sign in to comment.