Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly fallback when ERB column can't be computed #48184

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionview/lib/action_view/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def spot(location) # :nodoc:
# source location inside the template.
def translate_location(backtrace_location, spot)
if handler.respond_to?(:translate_location)
handler.translate_location(spot, backtrace_location, encode!)
handler.translate_location(spot, backtrace_location, encode!) || spot
else
spot
end
Expand Down
12 changes: 8 additions & 4 deletions actionview/lib/action_view/template/handlers/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ERB

ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")

LocationParsingError = Class.new(StandardError) # :nodoc:

def self.call(template, source)
new.call(template, source)
end
Expand Down Expand Up @@ -52,6 +54,8 @@ def translate_location(spot, backtrace_location, source)
spot[:script_lines] = source.lines

spot
rescue LocationParsingError
nil
end

def call(template, source)
Expand Down Expand Up @@ -110,15 +114,15 @@ def find_offset(compiled, source_tokens, error_column)
tok_name, str = *tok
case tok_name
when :TEXT
raise unless compiled.scan(str)
raise LocationParsingError unless compiled.scan(str)
when :CODE
raise "We went too far" if compiled.pos > error_column
raise LocationParsingError, "We went too far" if compiled.pos > error_column

if compiled.pos + str.bytesize >= error_column
offset = error_column - compiled.pos
return passed_tokens.map(&:last).join.bytesize + offset
else
raise unless compiled.scan(str)
raise LocationParsingError unless compiled.scan(str)
end
when :OPEN
next_tok = source_tokens.first.last
Expand All @@ -133,7 +137,7 @@ def find_offset(compiled, source_tokens, error_column)
compiled.getch
end
else
raise NotImplemented, tok.first
raise LocationParsingError, "Not implemented: #{tok.first}"
end

passed_tokens << tok
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Oh no!</h1>
This template has a runtime error
<b>
<% if true %>
<%= method_that_does_not_exist %>
<% end %>
</b>
Yikes!
13 changes: 13 additions & 0 deletions actionview/test/template/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ def test_render_runtime_error

assert_equal 6, translated_spot[:first_column]
end

def test_render_location_map_failure
ex = assert_raises(ActionView::Template::Error) {
@view.render(template: "test/unparseable_runtime_error")
}
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_not_nil translated_spot[:first_column]
end
end

def test_render_partial
Expand Down