Skip to content

Commit

Permalink
log errors when an exception happens when streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 1, 2011
1 parent 6960a23 commit 46611a9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions actionpack/lib/action_view/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Context
include CompiledTemplates
attr_accessor :output_buffer, :view_flow

# TODO Provide an easy method that initializes all variables?

# Returns the contents that are yielded to a layout, given a name or a block.
#
# You can think of a layout as a method that is called with a block. If the user calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,26 @@ def initialize(&start)
def each(&block)
begin
@start.call(block)
rescue
rescue Exception => exception
log_error(exception)
block.call ActionView::Base.streaming_completion_on_exception
end
self
end

private

# This is the same logging logic as in ShowExceptions middleware.
# TODO Once "exceptron" is in, refactor this piece to simply re-use exceptron.
def log_error(exception) #:nodoc:
logger = ActionController::Base.logger
return unless logger

message = "\n#{exception.class} (#{exception.message}):\n"
message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
message << " " << exception.backtrace.join("\n ")
logger.fatal("#{message}\n\n")
end
end

# For streaming, instead of rendering a given a template, we return a Body
Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/controller/new_base/render_streaming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ class StreamingTest < Rack::TestCase
assert_streaming!
end

test "rendering with template exception logs the exception" do
io = StringIO.new
_old, ActionController::Base.logger = ActionController::Base.logger, Logger.new(io)

begin
get "/render_streaming/basic/template_exception"
io.rewind
assert_match "(undefined method `invalid!' for nil:NilClass)", io.read
ensure
ActionController::Base.logger = _old
end
end

test "do not stream on HTTP/1.0" do
get "/render_streaming/basic/hello_world", nil, "HTTP_VERSION" => "HTTP/1.0"
assert_body "Hello world, I'm here!"
Expand Down
8 changes: 6 additions & 2 deletions actionpack/test/template/streaming_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ def setup
@controller_view = TestController.new.view_context
end

def render_body(options)
@view.view_renderer.render_body(@view, options)
end

def buffered_render(options)
body = @view.render_body(options)
body = render_body(options)
string = ""
body.each do |piece|
string << piece
Expand All @@ -24,7 +28,7 @@ def buffered_render(options)

def test_streaming_works
content = []
body = @view.render_body(:template => "test/hello_world.erb", :layout => "layouts/yield")
body = render_body(:template => "test/hello_world.erb", :layout => "layouts/yield")

body.each do |piece|
content << piece
Expand Down

0 comments on commit 46611a9

Please sign in to comment.