diff --git a/lib/exception_notification/rack.rb b/lib/exception_notification/rack.rb index 2ca86c5a..dcf4a3d2 100644 --- a/lib/exception_notification/rack.rb +++ b/lib/exception_notification/rack.rb @@ -5,6 +5,8 @@ def initialize(app, options = {}) ExceptionNotifier.ignored_exceptions = options.delete(:ignore_exceptions) if options.key?(:ignore_exceptions) + ExceptionNotifier.clean_backtrace = options.delete(:clean_backtrace) if options.key?(:clean_backtrace) + if options.key?(:ignore_if) rack_ignore = options.delete(:ignore_if) ExceptionNotifier.ignore_if do |exception, options| diff --git a/lib/exception_notifier.rb b/lib/exception_notifier.rb index f7539c90..e6a3d91d 100644 --- a/lib/exception_notifier.rb +++ b/lib/exception_notifier.rb @@ -22,6 +22,10 @@ class UndefinedNotifierError < StandardError; end mattr_accessor :ignored_exceptions @@ignored_exceptions = %w{ActiveRecord::RecordNotFound AbstractController::ActionNotFound ActionController::RoutingError ActionController::UnknownFormat} + # Setting for whether backtraces should be cleaned in the EmailNorifier + mattr_accessor :clean_backtrace + @@clean_backtrace = true + class << self # Store conditions that decide when exceptions must be ignored or not. @@ignores = [] diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index cf9fdec1..1fe69ff3 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -40,7 +40,7 @@ def background_exception_notification(exception, options={}, default_options={}) @exception = exception @options = options.reverse_merge(default_options) - @backtrace = exception.backtrace || [] + @backtrace = exception.backtrace ? clean_backtrace(exception) : [] @sections = @options[:background_sections] @data = options[:data] || {} @@ -65,7 +65,7 @@ def set_data_variables end def clean_backtrace(exception) - if defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) + if ExceptionNotifier.clean_backtrace && defined?(Rails) && Rails.respond_to?(:backtrace_cleaner) Rails.backtrace_cleaner.send(:filter, exception.backtrace) else exception.backtrace diff --git a/test/exception_notifier/email_notifier_test.rb b/test/exception_notifier/email_notifier_test.rb index 1b5d0573..655bbf01 100644 --- a/test/exception_notifier/email_notifier_test.rb +++ b/test/exception_notifier/email_notifier_test.rb @@ -2,17 +2,28 @@ class EmailNotifierTest < ActiveSupport::TestCase setup do + @clean_backtrace = ExceptionNotifier.clean_backtrace + ExceptionNotifier.clean_backtrace = true + Time.stubs(:current).returns('Sat, 20 Apr 2013 20:58:55 UTC +00:00') @email_notifier = ExceptionNotifier.registered_exception_notifier(:email) + begin 1/0 rescue => e @exception = e + + Rails.backtrace_cleaner.stubs(:filter).returns(@exception.backtrace.take(2)) + @mail = @email_notifier.create_email(@exception, :data => {:job => 'DivideWorkerJob', :payload => '1/0', :message => 'My Custom Message'}) end end + teardown do + ExceptionNotifier.clean_backtrace = @clean_backtrace + end + test "should have default sender address overridden" do assert @email_notifier.sender_address == %("Dummy Notifier" ) end @@ -113,8 +124,24 @@ class EmailNotifierTest < ActiveSupport::TestCase assert @vowel_mail.encoded.include? "An ActiveRecord::RecordNotFound occurred in background at #{Time.current}" end - test "mail should contain backtrace in body" do - assert @mail.encoded.include?("test/exception_notifier/email_notifier_test.rb:8"), "\n#{@mail.inspect}" + test "mail should contain cleaned backtrace in body" do + assert_include @mail.encoded, @exception.backtrace[0], "\n#{@mail}" + assert_include @mail.encoded, @exception.backtrace[1], "\n#{@mail}" + + assert_not_include @mail.encoded, @exception.backtrace[2], "\n#{@mail}" + assert_not_include @mail.encoded, @exception.backtrace[-1], "\n#{@mail}" + end + + test "mail should contain uncleaned backtrace in body if backtrace cleaning is disabled" do + ExceptionNotifier.clean_backtrace = false + + @mail = @email_notifier.create_email(@exception, + :data => {:job => 'DivideWorkerJob', :payload => '1/0', :message => 'My Custom Message'}) + + assert_include @mail.encoded, @exception.backtrace[0], "\n#{@mail.inspect}" + assert_include @mail.encoded, @exception.backtrace[1], "\n#{@mail.inspect}" + assert_include @mail.encoded, @exception.backtrace[2], "\n#{@mail.inspect}" + assert_include @mail.encoded, @exception.backtrace[-1], "\n#{@mail.inspect}" end test "mail should contain data in body" do