Permalink
Browse files

log errors when an exception happens when streaming.

  • Loading branch information...
1 parent 6960a23 commit 46611a995d91abf7bb2a64c62af13b6449c75b07 @josevalim josevalim committed May 1, 2011
@@ -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
@@ -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
@@ -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!"
@@ -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
@@ -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

0 comments on commit 46611a9

Please sign in to comment.