Skip to content
Open
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
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion exception_notification.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/exception_notification/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lib/exception_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notifier/email_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] || {}

Expand Down
3 changes: 2 additions & 1 deletion lib/exception_notifier/modules/backtrace_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions test/exception_notifier/email_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down