Skip to content

Commit

Permalink
Make common_logger plugin handle raised errors
Browse files Browse the repository at this point in the history
Internal after hooks are always called inside an ensure block,
even if there is no rack response.  So plugins that use internal
after hooks need to handle the case where there is no rack response.
  • Loading branch information
jeremyevans committed Sep 17, 2018
1 parent e0db64f commit a792cec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
= master

* Make common_logger plugin handle raised errors (jeremyevans)

= 3.12.0 (2018-09-14)

* Add common_logger plugin for common log support (jeremyevans)
Expand Down
2 changes: 2 additions & 0 deletions lib/roda/plugins/common_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ module InstanceMethods

# Log request/response information in common log format to logger.
def _roda_after_90__common_logger(result)
return unless result && result[0] && result[1]

elapsed_time = if timer = @_request_timer
'%0.4f' % (CommonLogger.start_timer - timer)
else
Expand Down
26 changes: 26 additions & 0 deletions spec/plugin/common_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,30 @@ def cl_app(&block)
@logger.rewind
@logger.read.must_match /\A- - - \[\d\d\/[A-Z][a-z]{2}\/\d\d\d\d:\d\d:\d\d:\d\d [-+]\d\d\d\d\] "GET \/ " 500 - 0.\d\d\d\d\n\z/
end

it 'does not log if an error is raised' do
cl_app do |r|
raise "foo"
end

begin
body
rescue => e
end
e.must_be_instance_of(RuntimeError)
e.message.must_equal 'foo'
end

it 'logs errors if used with error_handler' do
cl_app do |r|
raise "foo"
end
@app.plugin :error_handler do |_|
"bad"
end

body.must_equal 'bad'
@logger.rewind
@logger.read.must_match /\A- - - \[\d\d\/[A-Z][a-z]{2}\/\d\d\d\d:\d\d:\d\d:\d\d [-+]\d\d\d\d\] "GET \/ " 500 3 0.\d\d\d\d\n\z/
end
end

0 comments on commit a792cec

Please sign in to comment.