Skip to content

Commit

Permalink
filter_chain: prioritize library filter over user ones
Browse files Browse the repository at this point in the history
Fixes #195 (Question: Usage of add_filter)

We need to make sure that Airbrake library have higher precedence over
user filters to avoid confusion when expected data is not present in
the Notice object.
  • Loading branch information
kyrylo committed Apr 25, 2017
1 parent 1ae75d2 commit 0b56658
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/airbrake-ruby/filter_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module Airbrake
# @api private
# @since v1.0.0
class FilterChain
##
# @return [String] the namespace for reserved filters which are executed
# before any other filters
PRIORITY_FILTER_NAME = '#<Airbrake::'.freeze

##
# Filters to be executed last. By this time all permutations on a notice
# should be done, so the final step is to blacklist/whitelist keys.
Expand All @@ -24,21 +29,26 @@ def initialize(config)

[Airbrake::Filters::SystemExitFilter,
Airbrake::Filters::GemRootFilter].each do |filter|
add_filter(filter.new)
@filters << filter.new
end

root_directory = config.root_directory
return unless root_directory

add_filter(Airbrake::Filters::RootDirectoryFilter.new(root_directory))
@filters << Airbrake::Filters::RootDirectoryFilter.new(root_directory)
end

##
# Adds a filter to the filter chain.
#
# @param [#call] filter The filter object (proc, class, module, etc)
# @return [void]
def add_filter(filter)
return @keys_filters << filter if KEYS_FILTERS.include?(filter.class)
@filters << filter
return @filters << filter unless filter.to_s.start_with?(PRIORITY_FILTER_NAME)

i = @filters.rindex { |f| f.to_s.start_with?(PRIORITY_FILTER_NAME) }
@filters.insert(i + 1, filter) if i
end

##
Expand Down
16 changes: 16 additions & 0 deletions spec/filter_chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@
@chain.refine(notice)
expect(notice[:params][:bingo]).to eq('[Filtered]')
end

it "executes library filters before user ones" do
nums = []

@chain.add_filter(proc { nums << 2 })

priority_filter = proc { nums << 1 }
def priority_filter.to_s
'#<Airbrake::'
end
@chain.add_filter(priority_filter)

@chain.refine(notice)

expect(nums).to eq([1, 2])
end
end

describe "default backtrace filters" do
Expand Down

0 comments on commit 0b56658

Please sign in to comment.