From eb39d0f7b999f09c4e13f035634887a8f5592443 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 16 Jan 2010 21:34:35 -0600 Subject: Use backtrace cleaner for dev mode exception page --- .../lib/active_support/backtrace_cleaner.rb | 32 +++++++--- activesupport/lib/active_support/cache.rb | 8 +-- .../lib/active_support/core_ext/exception.rb | 47 --------------- activesupport/test/core_ext/exception_test.rb | 69 ---------------------- 4 files changed, 28 insertions(+), 128 deletions(-) delete mode 100644 activesupport/test/core_ext/exception_test.rb (limited to 'activesupport') 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 -- cgit v1.2.3 From 0334f9f6cfa4c4c746de7e19250a13366b616c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 17 Jan 2010 11:17:42 +0100 Subject: Add ActionDispatch::Notifications middleware. --- activesupport/lib/active_support/notifications.rb | 2 +- .../lib/active_support/notifications/instrumenter.rb | 9 +++++++++ activesupport/test/notifications_test.rb | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index 3e96decb8c..a1383bb478 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -45,7 +45,7 @@ module ActiveSupport class << self attr_writer :notifier delegate :publish, :subscribe, :to => :notifier - delegate :instrument, :to => :instrumenter + delegate :instrument, :instrument!, :to => :instrumenter def notifier @notifier ||= Notifier.new diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index f3d877efe7..7c5b118ee3 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -20,6 +20,15 @@ module ActiveSupport result end + # The same as instrument, but sends the notification even if the yielded + # block raises an error. + def instrument!(name, payload={}) + time = Time.now + yield(payload) if block_given? + ensure + @notifier.publish(name, time, Time.now, @id, payload) + end + private def unique_id SecureRandom.hex(10) diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index c3eb1a4eb5..c41d81fe7e 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -90,6 +90,22 @@ module Notifications drain end + def test_instrument_with_bang_returns_result_even_on_failure + begin + instrument!(:awesome, :payload => "notifications") do + raise "OMG" + end + flunk + rescue + end + + drain + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + def test_instrument_yields_the_paylod_for_further_modification assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } drain -- cgit v1.2.3