mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add handling and tests.
This commit is contained in:
parent
ac68550ae6
commit
07804d4759
4 changed files with 39 additions and 1 deletions
|
@ -313,6 +313,7 @@ module ActionView
|
|||
|
||||
# Make sure that the resulting String to be eval'd is in the
|
||||
# encoding of the code
|
||||
original_source = source
|
||||
source = +<<-end_src
|
||||
def #{method_name}(local_assigns, output_buffer)
|
||||
@virtual_path = #{@virtual_path.inspect};#{locals_code};#{code}
|
||||
|
@ -333,7 +334,14 @@ module ActionView
|
|||
raise WrongEncodingError.new(source, Encoding.default_internal)
|
||||
end
|
||||
|
||||
mod.module_eval(source, identifier, 0)
|
||||
begin
|
||||
mod.module_eval(source, identifier, 0)
|
||||
rescue SyntaxError
|
||||
# Account for when code in the template is not syntactically valid; e.g. if we're using
|
||||
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
|
||||
# the result into the template, but missing an end parenthesis.
|
||||
raise SyntaxErrorInTemplate.new(self, original_source)
|
||||
end
|
||||
end
|
||||
|
||||
def handle_render_error(view, e)
|
||||
|
|
|
@ -138,4 +138,24 @@ module ActionView
|
|||
end
|
||||
|
||||
TemplateError = Template::Error
|
||||
|
||||
class SyntaxErrorInTemplate < TemplateError #:nodoc
|
||||
def initialize(template, offending_code_string)
|
||||
@offending_code_string = offending_code_string
|
||||
super(template)
|
||||
end
|
||||
|
||||
def message
|
||||
<<~MESSAGE
|
||||
Encountered a syntax error while rendering template: check #{@offending_code_string}
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
def annoted_source_code
|
||||
@offending_code_string.split("\n").map.with_index(1) { |line, index|
|
||||
indentation = " " * 4
|
||||
"#{index}:#{indentation}#{line}"
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
4
actionview/test/fixtures/test/syntax_error.html.erb
vendored
Normal file
4
actionview/test/fixtures/test/syntax_error.html.erb
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
<%= foo(
|
||||
1,
|
||||
2,
|
||||
%>
|
|
@ -263,6 +263,12 @@ module RenderTestCases
|
|||
"and is followed by any combination of letters, numbers and underscores.", e.message
|
||||
end
|
||||
|
||||
def test_render_template_with_syntax_error
|
||||
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/syntax_error") }
|
||||
assert_match %r!syntax!, e.message
|
||||
assert_equal "1: <%= foo(", e.annoted_source_code[0].strip
|
||||
end
|
||||
|
||||
def test_render_partial_with_errors
|
||||
e = assert_raises(ActionView::Template::Error) { @view.render(partial: "test/raise") }
|
||||
assert_match %r!method.*doesnt_exist!, e.message
|
||||
|
|
Loading…
Reference in a new issue