Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/exception_notification/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
4 changes: 4 additions & 0 deletions lib/exception_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions lib/exception_notifier/email_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] || {}

Expand All @@ -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
Expand Down
31 changes: 29 additions & 2 deletions test/exception_notifier/email_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" <[email protected]>)
end
Expand Down Expand Up @@ -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
Expand Down