diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index db88dc0c..b4ba4c8f 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -4,6 +4,7 @@ * update URL in gemspec (by @ktdreyer) * Add `hostname` to Slack notifier (by @juanazam) * Allow `exception_recipients` to be a proc (by @kellyjosephprice) + * Add `clean_backtrace` option to allow displaying full backtrace in e-mail notifications (by @chancancode and @vala) == 4.1.4 @@ -108,7 +109,7 @@ * Add normalize_subject option to remove numbers from email so that they thread (by @jjb) * Allow the user to provide a custom message and hash of data (by @jjb) * Add support for configurable background sections and a data partial (by @jeffrafter) - * Include timestamp of exception in notification body + * Include timestamp of exception in notification body * Add support for rack based session management (by @phoet) * Add ignore_crawlers option to ignore exceptions generated by crawlers * Add verbode_subject option to exclude exception message from subject (by @amishyn) diff --git a/exception_notification.gemspec b/exception_notification.gemspec index 1b2bb77a..178d392a 100644 --- a/exception_notification.gemspec +++ b/exception_notification.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.add_dependency("actionmailer", "~> 4.0") s.add_dependency("activesupport", "~> 4.0") - s.add_development_dependency "rails", "~> 4.0" + s.add_development_dependency "rails", ">= 4.0", "<= 6.0" s.add_development_dependency "resque", "~> 1.2.0" # Sidekiq 3.2.2 does not support Ruby 1.9. s.add_development_dependency "sidekiq", "~> 3.0.0", "< 3.2.2" diff --git a/lib/exception_notification/rack.rb b/lib/exception_notification/rack.rb index 6ac907f6..65ccda69 100644 --- a/lib/exception_notification/rack.rb +++ b/lib/exception_notification/rack.rb @@ -6,6 +6,7 @@ def initialize(app, options = {}) @app = app 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) diff --git a/lib/exception_notifier.rb b/lib/exception_notifier.rb index f958ac57..2bbf4534 100644 --- a/lib/exception_notifier.rb +++ b/lib/exception_notifier.rb @@ -28,6 +28,9 @@ class UndefinedNotifierError < StandardError; end mattr_accessor :testing_mode @@testing_mode = false + 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 30668763..b4bafc24 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -45,7 +45,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] || {} diff --git a/lib/exception_notifier/modules/backtrace_cleaner.rb b/lib/exception_notifier/modules/backtrace_cleaner.rb index a2053a20..a77cae8f 100644 --- a/lib/exception_notifier/modules/backtrace_cleaner.rb +++ b/lib/exception_notifier/modules/backtrace_cleaner.rb @@ -2,7 +2,8 @@ module ExceptionNotifier module BacktraceCleaner 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 53688d89..ec20039a 100644 --- a/test/exception_notifier/email_notifier_test.rb +++ b/test/exception_notifier/email_notifier_test.rb @@ -3,6 +3,9 @@ class EmailNotifierTest < ActiveSupport::TestCase setup do + @original_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 @@ -14,6 +17,10 @@ class EmailNotifierTest < ActiveSupport::TestCase end end + teardown do + ExceptionNotifier.clean_backtrace = @original_clean_backtrace + end + test "should call pre/post_callback if specified" do assert_equal @email_notifier.options[:pre_callback_called], 1 assert_equal @email_notifier.options[:post_callback_called], 1 @@ -119,8 +126,24 @@ class EmailNotifierTest < ActiveSupport::TestCase assert_includes @vowel_mail.encoded, "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:9"), "\n#{@mail.inspect}" + test "mail should contain cleaned backtrace in body" do + assert_includes @mail.encoded, @exception.backtrace[0], "\n#{@mail}" + assert_includes @mail.encoded, @exception.backtrace[1], "\n#{@mail}" + + assert_not_includes @mail.encoded, @exception.backtrace[2], "\n#{@mail}" + assert_not_includes @mail.encoded, @exception.backtrace[-1], "\n#{@mail}" + end + + test "clean_backtrace should not do anything 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_includes @mail.encoded, @exception.backtrace[0], "\n#{@mail.inspect}" + assert_includes @mail.encoded, @exception.backtrace[1], "\n#{@mail.inspect}" + assert_includes @mail.encoded, @exception.backtrace[2], "\n#{@mail.inspect}" + assert_includes @mail.encoded, @exception.backtrace[-1], "\n#{@mail.inspect}" end test "mail should contain data in body" do