aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2010-01-16 21:34:35 -0600
committerJoshua Peek <josh@joshpeek.com>2010-01-16 21:34:35 -0600
commiteb39d0f7b999f09c4e13f035634887a8f5592443 (patch)
tree4155dd1bb0cb878ff0aec9f36c113e4a094ab498 /activesupport
parentd2d4acf02793580e0f0c1bc380389527325b6254 (diff)
downloadrails-eb39d0f7b999f09c4e13f035634887a8f5592443.tar.gz
rails-eb39d0f7b999f09c4e13f035634887a8f5592443.tar.bz2
rails-eb39d0f7b999f09c4e13f035634887a8f5592443.zip
Use backtrace cleaner for dev mode exception page
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/backtrace_cleaner.rb32
-rw-r--r--activesupport/lib/active_support/cache.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/exception.rb47
-rw-r--r--activesupport/test/core_ext/exception_test.rb69
4 files changed, 28 insertions, 128 deletions
diff --git a/activesupport/lib/active_support/backtrace_cleaner.rb b/activesupport/lib/active_support/backtrace_cleaner.rb
index 0e262c003e..6fab565646 100644
--- a/activesupport/lib/active_support/backtrace_cleaner.rb
+++ b/activesupport/lib/active_support/backtrace_cleaner.rb
@@ -9,7 +9,7 @@ module ActiveSupport
# Example:
#
# bc = BacktraceCleaner.new
- # bc.add_filter { |line| line.gsub(Rails.root, '') }
+ # bc.add_filter { |line| line.gsub(Rails.root, '') }
# bc.add_silencer { |line| line =~ /mongrel|rubygems/ }
# bc.clean(exception.backtrace) # will strip the Rails.root prefix and skip any lines from mongrel or rubygems
#
@@ -18,10 +18,19 @@ module ActiveSupport
def initialize
@filters, @silencers = [], []
end
-
+
# Returns the backtrace after all filters and silencers has been run against it. Filters run first, then silencers.
- def clean(backtrace)
- silence(filter(backtrace))
+ def clean(backtrace, kind = :silent)
+ filtered = filter(backtrace)
+
+ case kind
+ when :silent
+ silence(filtered)
+ when :noise
+ noise(filtered)
+ else
+ filtered
+ end
end
# Adds a filter from the block provided. Each line in the backtrace will be mapped against this filter.
@@ -51,21 +60,28 @@ module ActiveSupport
@silencers = []
end
-
private
def filter(backtrace)
@filters.each do |f|
backtrace = backtrace.map { |line| f.call(line) }
end
-
+
backtrace
end
-
+
def silence(backtrace)
@silencers.each do |s|
backtrace = backtrace.reject { |line| s.call(line) }
end
-
+
+ backtrace
+ end
+
+ def noise(backtrace)
+ @silencers.each do |s|
+ backtrace = backtrace.select { |line| s.call(line) }
+ end
+
backtrace
end
end
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 6360a4614e..7213b24f2d 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -30,7 +30,7 @@ module ActiveSupport
#
# ActiveSupport::Cache.lookup_store(:memory_store)
# # => returns a new ActiveSupport::Cache::MemoryStore object
- #
+ #
# ActiveSupport::Cache.lookup_store(:mem_cache_store)
# # => returns a new ActiveSupport::Cache::MemCacheStore object
#
@@ -97,7 +97,7 @@ module ActiveSupport
# Ruby objects, but don't count on every cache store to be able to do that.
#
# cache = ActiveSupport::Cache::MemoryStore.new
- #
+ #
# cache.read("city") # => nil
# cache.write("city", "Duckburgh")
# cache.read("city") # => "Duckburgh"
@@ -139,7 +139,7 @@ module ActiveSupport
#
# cache.write("today", "Monday")
# cache.fetch("today") # => "Monday"
- #
+ #
# cache.fetch("city") # => nil
# cache.fetch("city") do
# "Duckburgh"
@@ -198,7 +198,7 @@ module ActiveSupport
# You may also specify additional options via the +options+ argument.
# The specific cache store implementation will decide what to do with
# +options+.
- #
+ #
# For example, MemCacheStore supports the +:expires_in+ option, which
# tells the memcached server to automatically expire the cache item after
# a certain period:
diff --git a/activesupport/lib/active_support/core_ext/exception.rb b/activesupport/lib/active_support/core_ext/exception.rb
index b594fbae8e..ef801e713d 100644
--- a/activesupport/lib/active_support/core_ext/exception.rb
+++ b/activesupport/lib/active_support/core_ext/exception.rb
@@ -1,50 +1,3 @@
module ActiveSupport
FrozenObjectError = RUBY_VERSION < '1.9' ? TypeError : RuntimeError
end
-
-# TODO: Turn all this into using the BacktraceCleaner.
-class Exception # :nodoc:
- # Clean the paths contained in the message.
- def self.clean_paths(string)
- require 'pathname' unless defined? Pathname
- string.gsub(%r{[\w. ]+(/[\w. ]+)+(\.rb)?(\b|$)}) do |path|
- Pathname.new(path).cleanpath
- end
- end
-
- def clean_message
- Exception.clean_paths(message)
- end
-
- TraceSubstitutions = []
- FrameworkStart = /action_controller\/dispatcher\.rb/.freeze
- FrameworkRegexp = /generated|vendor|dispatch|ruby|script\/\w+/.freeze
-
- def clean_backtrace
- backtrace.collect do |line|
- substituted = TraceSubstitutions.inject(line) do |result, (regexp, sub)|
- result.gsub regexp, sub
- end
- Exception.clean_paths(substituted)
- end
- end
-
- def application_backtrace
- before_framework_frame = nil
- before_application_frame = true
-
- trace = clean_backtrace.reject do |line|
- before_framework_frame ||= (line =~ FrameworkStart)
- non_app_frame = (line =~ FrameworkRegexp)
- before_application_frame = false unless non_app_frame
- before_framework_frame || (non_app_frame && !before_application_frame)
- end
-
- # If we didn't find any application frames, return an empty app trace.
- before_application_frame ? [] : trace
- end
-
- def framework_backtrace
- clean_backtrace.grep FrameworkRegexp
- end
-end
diff --git a/activesupport/test/core_ext/exception_test.rb b/activesupport/test/core_ext/exception_test.rb
deleted file mode 100644
index e63842c0bd..0000000000
--- a/activesupport/test/core_ext/exception_test.rb
+++ /dev/null
@@ -1,69 +0,0 @@
-require 'abstract_unit'
-require 'active_support/core_ext/exception'
-
-class ExceptionExtTests < Test::Unit::TestCase
-
- def get_exception(cls = RuntimeError, msg = nil, trace = nil)
- begin raise cls, msg, (trace || caller)
- rescue Exception => e # passed Exception
- return e
- end
- end
-
- def setup
- Exception::TraceSubstitutions.clear
- end
-
- def test_clean_backtrace
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['bhal.rb', 'rawh hid den stuff is not here', 'almost all']
- assert_kind_of Exception, e
- assert_equal ['bhal.rb', 'rawh hid den stuff is not here', 'almost all'], e.clean_backtrace
- end
-
- def test_app_backtrace
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['bhal.rb', ' vendor/file.rb some stuff', 'almost all']
- assert_kind_of Exception, e
- assert_equal ['bhal.rb', 'almost all'], e.application_backtrace
- end
-
- def test_app_backtrace_with_before
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['vendor/file.rb some stuff', 'bhal.rb', ' vendor/file.rb some stuff', 'almost all']
- assert_kind_of Exception, e
- assert_equal ['vendor/file.rb some stuff', 'bhal.rb', 'almost all'], e.application_backtrace
- end
-
- def test_framework_backtrace_with_before
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['vendor/file.rb some stuff', 'bhal.rb', ' vendor/file.rb some stuff', 'almost all']
- assert_kind_of Exception, e
- assert_equal ['vendor/file.rb some stuff', ' vendor/file.rb some stuff'], e.framework_backtrace
- end
-
- def test_backtrace_should_clean_paths
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['a/b/c/../d/../../../bhal.rb', 'rawh hid den stuff is not here', 'almost all']
- assert_kind_of Exception, e
- assert_equal ['bhal.rb', 'rawh hid den stuff is not here', 'almost all'], e.clean_backtrace
- end
-
- def test_clean_message_should_clean_paths
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, "I dislike a/z/x/../../b/y/../c", ['a/b/c/../d/../../../bhal.rb', 'rawh hid den stuff is not here', 'almost all']
- assert_kind_of Exception, e
- assert_equal "I dislike a/b/c", e.clean_message
- end
-
- def test_app_trace_should_be_empty_when_no_app_frames
- Exception::TraceSubstitutions << [/\s*hidden.*/, '']
- e = get_exception RuntimeError, 'RAWR', ['vendor/file.rb some stuff', 'generated/bhal.rb', ' vendor/file.rb some stuff', 'generated/almost all']
- assert_kind_of Exception, e
- assert_equal [], e.application_backtrace
- end
-
- def test_frozen_error
- assert_raise(ActiveSupport::FrozenObjectError) { "foo".freeze.gsub!(/oo/,'aa') }
- end
-end