Skip to content

Commit

Permalink
Display halted callbacks in log for Application Controller
Browse files Browse the repository at this point in the history
Fix roidrage#258

Sometimes when you do the final render or redirection, it can be halted by a controller callback. With the existing code, it can very hard to understand where it stopped.

With this commit, the intent is to provide the method name where it stops.
  • Loading branch information
benoittgt committed Sep 28, 2023
1 parent f6ca5bc commit 6268775
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/lograge/log_subscribers/action_controller.rb
Expand Up @@ -70,6 +70,14 @@ def extract_unpermitted_params
RequestStore.store[:lograge_unpermitted_params] = nil
{ unpermitted_params: unpermitted_params }
end

def extract_halted_callback
halted_callback = RequestStore.store[:lograge_halted_callback]
return {} unless halted_callback

RequestStore.store[:lograge_halted_callback] = nil
{ halted_callback: halted_callback }
end
end
end
end
3 changes: 2 additions & 1 deletion lib/lograge/log_subscribers/base.rb
Expand Up @@ -33,11 +33,12 @@ def extract_request(event, payload)
data.merge!(extract_runtimes(event, payload))
data.merge!(extract_location)
data.merge!(extract_unpermitted_params)
data.merge!(extract_halted_callback)
data.merge!(custom_options(event))
end

%i[initial_data extract_status extract_runtimes
extract_location extract_unpermitted_params].each do |method_name|
extract_location extract_unpermitted_params extract_halted_callback].each do |method_name|
define_method(method_name) { |*_arg| {} }
end

Expand Down
23 changes: 22 additions & 1 deletion spec/log_subscribers/action_controller_spec.rb
Expand Up @@ -189,7 +189,28 @@
expect(log_output.string).to_not include('location=')
end

context 'with unpermitted_parameters' do
context 'with halted_callback' do
before do
RequestStore.store[:lograge_halted_callback] = 'user_authenticated'
end

it 'adds the halted_callback to the log line' do
subscriber.process_action(event)
expect(log_output.string).to include('halted_callback=user_authenticated')
end

it 'removes the thread local variable' do
subscriber.process_action(event)
expect(RequestStore.store[:lograge_halted_callback]).to be_nil
end
end

it 'does not include halted_callback by default' do
subscriber.process_action(event)
expect(log_output.string).to_not include('halted_callback=')
end

context 'with unpermitted_params' do
before do
RequestStore.store[:lograge_unpermitted_params] = %w[florb blarf]
end
Expand Down

0 comments on commit 6268775

Please sign in to comment.