Skip to content

Commit

Permalink
Merge pull request #38065 from eugeneius/6_0_stable_template_syntax_e…
Browse files Browse the repository at this point in the history
…rror

Backport #36521 and #36532 to 6-0-stable
  • Loading branch information
kaspth committed Dec 22, 2019
2 parents ea5749c + e350c4d commit e2cfa05
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
10 changes: 10 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* annotated_source_code returns an empty array so TemplateErrors without a
template in the backtrace are surfaced properly by DebugExceptions.

*Guilherme Mansur*, *Kasper Timm Hansen*

* Add autoload for SyntaxErrorInTemplate so syntax errors are correctly raised by DebugExceptions.

*Guilherme Mansur*, *Gannon McGibbon*


## Rails 6.0.2.1 (December 18, 2019) ##

* No changes.
Expand Down
1 change: 1 addition & 0 deletions actionview/lib/action_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module ActionView
autoload :ActionViewError
autoload :EncodingError
autoload :TemplateError
autoload :SyntaxErrorInTemplate
autoload :WrongEncodingError
end
end
Expand Down
20 changes: 8 additions & 12 deletions actionview/lib/action_view/template/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def sub_template_message
end
end

def source_extract(indentation = 0, output = :console)
return unless num = line_number
def source_extract(indentation = 0)
return [] unless num = line_number
num = num.to_i

source_code = @template.source.split("\n")
Expand All @@ -91,9 +91,9 @@ def source_extract(indentation = 0, output = :console)
end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min

indent = end_on_line.to_s.size + indentation
return unless source_code = source_code[start_on_line..end_on_line]
return [] unless source_code = source_code[start_on_line..end_on_line]

formatted_code_for(source_code, start_on_line, indent, output)
formatted_code_for(source_code, start_on_line, indent)
end

def sub_template_of(template_path)
Expand Down Expand Up @@ -122,15 +122,11 @@ def source_location
end + file_name
end

def formatted_code_for(source_code, line_counter, indent, output)
start_value = (output == :html) ? {} : []
source_code.inject(start_value) do |result, line|
def formatted_code_for(source_code, line_counter, indent)
indent_template = "%#{indent}s: %s"
source_code.map do |line|
line_counter += 1
if output == :html
result.update(line_counter.to_s => "%#{indent}s %s\n" % ["", line])
else
result << "%#{indent}s: %s" % [line_counter, line]
end
indent_template % [line_counter, line]
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions actionview/test/template/template_error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ def test_provides_useful_inspect

assert_equal "#<ActionView::Template::Error: original>", error.inspect
end

def test_annotated_source_code_returns_empty_array_if_source_cant_be_found
template = Class.new do
def identifier
"something"
end
end.new

error = begin
raise
rescue
raise ActionView::Template::Error.new(template) rescue $!
end

assert_equal [], error.annotated_source_code
end
end

0 comments on commit e2cfa05

Please sign in to comment.