Merge pull request #38059 from eugeneius/source_extract_encode_template

Encode template source before extracting lines
This commit is contained in:
Kasper Timm Hansen 2019-12-22 08:56:29 +01:00 committed by GitHub
commit 6047301063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -61,7 +61,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
def call(env)
env["action_dispatch.show_detailed_exceptions"] = @detailed
req = ActionDispatch::Request.new(env)
template = ActionView::Template.new(File.read(__FILE__), __FILE__, ActionView::Template::Handlers::Raw.new, format: :html, locals: [])
template = ActionView::Template.new(File.binread(__FILE__), __FILE__, ActionView::Template::Handlers::Raw.new, format: :html, locals: [])
case req.path
when "/pass"
@ -118,6 +118,12 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
raise CustomActionableError
when %r{/nil_annoted_source_code_error}
method_that_raises_nil_annoted_source_code
when "/utf8_template_error"
begin
eval "“fancy string”"
rescue Exception
raise ActionView::Template::Error.new(template)
end
else
raise "puke!"
end
@ -686,4 +692,17 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
assert_select "header h1", /DebugExceptionsTest::Boomer::NilAnnotedSourceCodeError/
assert_select "#container h2", /nil annoted_source_code/
end
test "debug exceptions app shows diagnostics for template errors that contain UTF-8 characters" do
@app = DevelopmentApp
io = StringIO.new
logger = ActiveSupport::Logger.new(io)
get "/utf8_template_error", headers: { "action_dispatch.logger" => logger }
assert_response 500
assert_select "#container p", /Showing #{__FILE__} where line #\d+ raised/
assert_select "#container code", /undefined local variable or method `string”'/
end
end

View File

@ -85,7 +85,7 @@ module ActionView
return [] unless num = line_number
num = num.to_i
source_code = @template.source.split("\n")
source_code = @template.encode!.split("\n")
start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min