Skip to content

Commit

Permalink
Merge pull request smartinez87#26 from amishyn/master
Browse files Browse the repository at this point in the history
Added option to exclude exception.message from subject
  • Loading branch information
smartinez87 committed Oct 1, 2011
2 parents 1c6ff2b + e4ea974 commit 5c4460f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ run on production. You can make it work by

Customization
---

By default subject includes exception message. Use :verbose_subject => false
to exclude it from subject.
By default, the notification email includes four parts: request, session,
environment, and backtrace (in that order). You can customize how each of those
sections are rendered by placing a partial named for that part in your
Expand Down
17 changes: 11 additions & 6 deletions lib/exception_notifier/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def default_options
{ :sender_address => default_sender_address,
:exception_recipients => default_exception_recipients,
:email_prefix => default_email_prefix,
:sections => default_sections }
:sections => default_sections,
:verbose_subject => true }
end
end

Expand All @@ -58,9 +59,7 @@ def exception_notification(env, exception)
instance_variable_set("@#{name}", value)
end

prefix = "#{@options[:email_prefix]}#{@kontroller.controller_name}##{@kontroller.action_name}"
subject = "#{prefix} (#{@exception.class}) #{@exception.message.inspect}"
subject = subject.length > 120 ? subject[0...120] + "..." : subject
subject = compose_subject("#{@kontroller.controller_name}##{@kontroller.action_name}")

mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
format.text { render "#{mailer_name}/exception_notification" }
Expand All @@ -70,11 +69,10 @@ def exception_notification(env, exception)
def background_exception_notification(exception)
if @notifier = Rails.application.config.middleware.detect{ |x| x.klass == ExceptionNotifier }
@options = (@notifier.args.first || {}).reverse_merge(self.class.default_options)
subject = "#{@options[:email_prefix]} (#{exception.class}) #{exception.message.inspect}"

@exception = exception
@backtrace = exception.backtrace || []
@sections = %w{backtrace}
subject = compose_subject"#{@options[:email_prefix]} (#{exception.class}) #{exception.message.inspect}"

mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
format.text { render "#{mailer_name}/background_exception_notification" }
Expand All @@ -84,6 +82,13 @@ def background_exception_notification(exception)

private

def compose_subject(name = '')
subject = "#{@options[:email_prefix]}#{name}"
subject << " (#{@exception.class})"
subject << " #{@exception.message.inspect}" if @options[:verbose_subject]
subject.length > 120 ? subject[0...120] + "..." : subject
end

def clean_backtrace(exception)
if Rails.respond_to?(:backtrace_cleaner)
Rails.backtrace_cleaner.send(:filter, exception.backtrace)
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../../..
specs:
exception_notification (2.4.1)
exception_notification (2.5.2)
actionmailer (>= 3.0.4)

GEM
Expand Down
14 changes: 14 additions & 0 deletions test/dummy/test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@ class PostsControllerTest < ActionController::TestCase
assert request.ssl?
assert @secured_mail.body.include? "* session id: [FILTERED]\n *"
end

test "should not include exception message in subject" do
begin
@post = posts(:one)
post :create, :post => @post.attributes
rescue => e
@exception = e
custom_env = request.env
custom_env['exception_notifier.options']||={}
custom_env['exception_notifier.options'].merge!(:verbose_subject => false)
@mail = ExceptionNotifier::Notifier.exception_notification(custom_env, @exception)
end
assert_equal "[Dummy ERROR] # (NoMethodError)", @mail.subject
end
end

0 comments on commit 5c4460f

Please sign in to comment.