Skip to content

Commit

Permalink
filter_chain: factor out filters to separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo committed Apr 26, 2017
1 parent 23a199c commit e91048a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 43 deletions.
3 changes: 3 additions & 0 deletions lib/airbrake-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
require 'airbrake-ruby/filters/keys_filter'
require 'airbrake-ruby/filters/keys_whitelist'
require 'airbrake-ruby/filters/keys_blacklist'
require 'airbrake-ruby/filters/gem_root_filter'
require 'airbrake-ruby/filters/system_exit_filter'
require 'airbrake-ruby/filters/root_directory_filter'
require 'airbrake-ruby/filter_chain'
require 'airbrake-ruby/notifier'

Expand Down
49 changes: 6 additions & 43 deletions lib/airbrake-ruby/filter_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@ module Airbrake
# @api private
# @since v1.0.0
class FilterChain
##
# Replaces paths to gems with a placeholder.
# @return [Proc]
GEM_ROOT_FILTER = proc do |notice|
return unless defined?(Gem)

notice[:errors].each do |error|
Gem.path.each do |gem_path|
error[:backtrace].each do |frame|
# If the frame is unparseable, then 'file' is nil, thus nothing to
# filter (all frame's data is in 'function' instead).
next unless (file = frame[:file])
file.sub!(/\A#{gem_path}/, '[GEM_ROOT]'.freeze)
end
end
end
end

##
# 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 @@ -34,27 +16,21 @@ class FilterChain
Airbrake::Filters::KeysWhitelist
].freeze

##
# Skip over SystemExit exceptions, because they're just noise.
# @return [Proc]
SYSTEM_EXIT_FILTER = proc do |notice|
if notice[:errors].any? { |error| error[:type] == 'SystemExit' }
notice.ignore!
end
end

##
# @param [Airbrake::Config] config
def initialize(config)
@filters = []
@keys_filters = []

[SYSTEM_EXIT_FILTER, GEM_ROOT_FILTER].each do |filter|
add_filter(filter)
[Airbrake::Filters::SystemExitFilter,
Airbrake::Filters::GemRootFilter].each do |filter|
add_filter(filter.new)
end

root_directory = config.root_directory
add_filter(root_directory_filter(root_directory)) if root_directory
return unless root_directory

add_filter(Airbrake::Filters::RootDirectoryFilter.new(root_directory))
end

##
Expand All @@ -77,18 +53,5 @@ def refine(notice)
filter.call(notice)
end
end

private

def root_directory_filter(root_directory)
proc do |notice|
notice[:errors].each do |error|
error[:backtrace].each do |frame|
next unless (file = frame[:file])
file.sub!(/\A#{root_directory}/, '[PROJECT_ROOT]'.freeze)
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions lib/airbrake-ruby/filters/gem_root_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Airbrake
module Filters
##
# Replaces paths to gems with a placeholder.
class GemRootFilter
##
# @return [String]
GEM_ROOT_LABEL = '[GEM_ROOT]'.freeze

def call(notice)
return unless defined?(Gem)

notice[:errors].each do |error|
Gem.path.each do |gem_path|
error[:backtrace].each do |frame|
# If the frame is unparseable, then 'file' is nil, thus nothing to
# filter (all frame's data is in 'function' instead).
next unless (file = frame[:file])
file.sub!(/\A#{gem_path}/, GEM_ROOT_LABEL)
end
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/airbrake-ruby/filters/root_directory_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Airbrake
module Filters
##
# Replaces root directory with a label.
class RootDirectoryFilter
##
# @return [String]
PROJECT_ROOT_LABEL = '[PROJECT_ROOT]'.freeze

def initialize(root_directory)
@root_directory = root_directory
end

def call(notice)
notice[:errors].each do |error|
error[:backtrace].each do |frame|
next unless (file = frame[:file])
file.sub!(/\A#{@root_directory}/, PROJECT_ROOT_LABEL)
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/airbrake-ruby/filters/system_exit_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Airbrake
module Filters
##
# Skip over SystemExit exceptions, because they're just noise.
class SystemExitFilter
##
# @return [String]
SYSTEM_EXIT_TYPE = 'SystemExit'.freeze

def call(notice)
return if notice[:errors].none? { |error| error[:type] == SYSTEM_EXIT_TYPE }
notice.ignore!
end
end
end
end

0 comments on commit e91048a

Please sign in to comment.