Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Removing backtrace lines for hoptoad_notifier when placed in vendor/p…
Browse files Browse the repository at this point in the history
…lugins or vendor/gems
  • Loading branch information
qrush committed Mar 12, 2009
1 parent 6cb8734 commit 4b879b4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
42 changes: 28 additions & 14 deletions lib/hoptoad_notifier.rb
Expand Up @@ -20,14 +20,18 @@ module HoptoadNotifier
class << self
attr_accessor :host, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass
attr_reader :backtrace_filters
#attr_reader :backtrace_filters

def backtrace_filters
@backtrace_filters ||= []
end

# Takes a block and adds it to the list of backtrace filters. When the filters
# run, the block will be handed each line of the backtrace and can modify
# it as necessary. For example, by default a path matching the RAILS_ROOT
# constant will be transformed into "[RAILS_ROOT]"
def filter_backtrace &block
(@backtrace_filters ||= []) << block
self.backtrace_filters << block
end

# The port on which your Hoptoad server runs.
Expand Down Expand Up @@ -95,6 +99,8 @@ def environment_filters
#
# NOTE: secure connections are not yet supported.
def configure
self.backtrace_filters.clear
add_default_filters
yield self
if defined?(ActionController::Base) && !ActionController::Base.include?(HoptoadNotifier::Catcher)
ActionController::Base.send(:include, HoptoadNotifier::Catcher)
Expand Down Expand Up @@ -133,20 +139,26 @@ def default_notice_options #:nodoc:
def notify notice = {}
Sender.new.notify_hoptoad( notice )
end
end

filter_backtrace do |line|
line.gsub(/#{RAILS_ROOT}/, "[RAILS_ROOT]")
end
def add_default_filters
filter_backtrace do |line|
line.gsub(/#{RAILS_ROOT}/, "[RAILS_ROOT]")
end

filter_backtrace do |line|
line.gsub(/^\.\//, "")
end
filter_backtrace do |line|
line.gsub(/^\.\//, "")
end

filter_backtrace do |line|
if defined?(Gem)
Gem.path.inject(line) do |line, path|
line.gsub(/#{path}/, "[GEM_ROOT]")
filter_backtrace do |line|
if defined?(Gem)
Gem.path.inject(line) do |line, path|
line.gsub(/#{path}/, "[GEM_ROOT]")
end
end
end

filter_backtrace do |line|
line if line !~ /^vendor\/(plugins|gems)\/hoptoad_notifier/
end
end
end
Expand Down Expand Up @@ -291,11 +303,13 @@ def clean_hoptoad_backtrace backtrace #:nodoc:
backtrace = backtrace.to_a.first.split(/\n\s*/)
end

backtrace.to_a.map do |line|
filtered = backtrace.to_a.map do |line|
HoptoadNotifier.backtrace_filters.inject(line) do |line, proc|
proc.call(line)
end
end

filtered.compact
end

def clean_hoptoad_params params #:nodoc:
Expand Down
32 changes: 32 additions & 0 deletions test/configuration_test.rb
Expand Up @@ -49,6 +49,38 @@ def rescue_action e
assert_equal "hoptoadapp.com", HoptoadNotifier.host
end

context "clean backtraces" do
setup do
HoptoadNotifier.configure {}
end

should "remove trace from vendor/gems" do
dirty_backtrace =
["vendor/gems/hoptoad_notifier-0.1.1/lib/hoptoad_notifier.rb:237:in `normalize_notice'",
"vendor/gems/hoptoad_notifier-0.1.1/lib/hoptoad_notifier.rb:175:in `notify_hoptoad'",
"app/controllers/posts_controller.rb:6:in `index'"]

clean_backtrace = @controller.send(:clean_hoptoad_backtrace, dirty_backtrace)
clean_backtrace.each do |line|
assert_no_match /^vendor\/gems\/hoptoad_notifier/, line
end
assert_equal 1, clean_backtrace.size
end

should "remove trace from vendor/plugins" do
dirty_backtrace =
["vendor/plugins/hoptoad_notifier/lib/hoptoad_notifier.rb:237:in `normalize_notice'",
"vendor/plugins/hoptoad_notifier/lib/hoptoad_notifier.rb:175:in `notify_hoptoad'",
"app/controllers/posts_controller.rb:6:in `index'"]

clean_backtrace = @controller.send(:clean_hoptoad_backtrace, dirty_backtrace)
clean_backtrace.each do |line|
assert_no_match /^vendor\/plugins\/hoptoad_notifier/, line
end
assert_equal 1, clean_backtrace.size
end
end

should "add filters to the backtrace_filters" do
assert_difference "HoptoadNotifier.backtrace_filters.length" do
HoptoadNotifier.configure do |config|
Expand Down

0 comments on commit 4b879b4

Please sign in to comment.