Fix DebugExceptions crash on nil Exception#annoted_source_code

The issue was reported in #29537. We expect Exception#annoted_source_code
to return an `Array`, but this contract may not be followed by all the
implementers.

Fixes #29537.
This commit is contained in:
Genadi Samokovarov 2017-06-22 18:52:53 +03:00 committed by Rafael Mendonça França
parent 3c7c2d220e
commit 7465b383ea
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 31 additions and 2 deletions

View File

@ -134,6 +134,7 @@ module ActionDispatch
def log_error(request, wrapper)
logger = logger(request)
return unless logger
exception = wrapper.exception
@ -152,10 +153,14 @@ module ActionDispatch
end
def log_array(logger, array)
lines = Array(array)
return if lines.empty?
if logger.formatter && logger.formatter.respond_to?(:tags_text)
logger.fatal array.join("\n#{logger.formatter.tags_text}")
logger.fatal lines.join("\n#{logger.formatter.tags_text}")
else
logger.fatal array.join("\n")
logger.fatal lines.join("\n")
end
end

View File

@ -20,6 +20,12 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
class Boomer
attr_accessor :closed
class NilAnnotedSourceCodeError < StandardError
def annoted_source_code
nil
end
end
def initialize(detailed = false)
@detailed = detailed
@closed = false
@ -48,6 +54,10 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
end
end
def method_that_raises_nil_annoted_source_code
raise NilAnnotedSourceCodeError, "nil annoted_source_code"
end
def call(env)
env["action_dispatch.show_detailed_exceptions"] = @detailed
req = ActionDispatch::Request.new(env)
@ -106,6 +116,8 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
raise_nested_exceptions
when %r{/actionable_error}
raise CustomActionableError
when %r{/nil_annoted_source_code_error}
method_that_raises_nil_annoted_source_code
else
raise "puke!"
end
@ -662,4 +674,16 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
assert_response 400
assert_match "ActionController::BadRequest", body
end
test "debug exceptions with misbehaving Exception#annoted_source_code" do
@app = DevelopmentApp
io = StringIO.new
logger = ActiveSupport::Logger.new(io)
get "/nil_annoted_source_code_error", headers: { "action_dispatch.show_exceptions" => true, "action_dispatch.logger" => logger }
assert_select "header h1", /DebugExceptionsTest::Boomer::NilAnnotedSourceCodeError/
assert_select "#container h2", /nil annoted_source_code/
end
end