Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch ActionDispatch::DebugExceptions to capture NotFound exceptions #252

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/lograge.rb
Expand Up @@ -117,6 +117,7 @@ def unsubscribe(component, subscriber)
end

def setup(app)
require 'lograge/rails_ext/action_dispatch/debug_exceptions'
self.application = app
disable_rack_cache_verbose_output
keep_original_rails_log
Expand Down
1 change: 1 addition & 0 deletions lib/lograge/log_subscriber.rb
Expand Up @@ -15,6 +15,7 @@ def process_action(event)
formatted_message = Lograge.formatter.call(data)
logger.send(Lograge.log_level, formatted_message)
end
alias process_exception process_action

def redirect_to(event)
RequestStore.store[:lograge_location] = event.payload[:location]
Expand Down
17 changes: 17 additions & 0 deletions lib/lograge/rails_ext/action_dispatch/debug_exceptions.rb
@@ -0,0 +1,17 @@
require 'action_dispatch/middleware/debug_exceptions'

module Lograge
module DebugExceptions
def log_error(request, wrapper)
payload = {
path: request.fullpath,
method: request.method,
format: request.format.ref,
exception: [wrapper.exception.class.name, wrapper.exception.message]
}
ActiveSupport::Notifications.instrument 'process_exception.action_controller', payload
super(request, wrapper) if Lograge.lograge_config.keep_original_rails_log
end
end
ActionDispatch::DebugExceptions.prepend DebugExceptions
end
58 changes: 58 additions & 0 deletions spec/lograge_spec.rb
Expand Up @@ -212,4 +212,62 @@ def current_user_id
end
end
end

describe 'handling exceptions' do
let(:app_config) do
double(config:
ActiveSupport::OrderedOptions.new.tap do |config|
config.action_dispatch = double(rack_cache: false)
config.lograge = ActiveSupport::OrderedOptions.new
end)
end
let(:debug_exceptions) do
# Workaround `undefined method 'blamed_files'` bug
require 'active_support/dependencies'
# Workaround `undefined method 'with_indifferent_access'` bug:
# https://github.com/rails/rails/issues/33634
require 'active_support/core_ext/hash/indifferent_access'
ActionDispatch::DebugExceptions.new(->(_) { raise })
end
let(:output) { StringIO.new }
let(:logger) { Logger.new(output) }
let(:env) do
Rack::MockRequest.env_for(
'',
'action_dispatch.show_detailed_exceptions' => true,
'action_dispatch.logger' => logger
)
end

before do
Lograge.setup(app_config)
Lograge.logger = logger
end

it 'adds formatted exception log' do
debug_exceptions.call(env)
expect(output.string).to match(/status=500 error='RuntimeError: '/)
end

it 'removes original exception log' do
debug_exceptions.call(env)
expect(output.string).not_to match(/FATAL -- : RuntimeError/)
end

context 'when keep_original_rails_log is true' do
before do
app_config.config.lograge.keep_original_rails_log = true
end

it 'adds formatted exception log' do
debug_exceptions.call(env)
expect(output.string).to match(/status=500 error='RuntimeError: '/)
end

it 'keeps original exception log' do
debug_exceptions.call(env)
expect(output.string).to match(/FATAL -- : RuntimeError/)
end
end
end
end