Skip to content

Commit

Permalink
Refactor verbose_subject option.
Browse files Browse the repository at this point in the history
  • Loading branch information
smartinez87 committed Oct 1, 2011
1 parent 5c4460f commit 1c2af60
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lib/exception_notifier/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def exception_notification(env, exception)
instance_variable_set("@#{name}", value)
end

subject = compose_subject("#{@kontroller.controller_name}##{@kontroller.action_name}")
subject = compose_subject(exception, @kontroller)

mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
format.text { render "#{mailer_name}/exception_notification" }
Expand All @@ -72,7 +72,7 @@ def background_exception_notification(exception)
@exception = exception
@backtrace = exception.backtrace || []
@sections = %w{backtrace}
subject = compose_subject"#{@options[:email_prefix]} (#{exception.class}) #{exception.message.inspect}"
subject = compose_subject(exception)

mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
format.text { render "#{mailer_name}/background_exception_notification" }
Expand All @@ -82,13 +82,14 @@ 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]
def compose_subject(exception, kontroller=nil)
subject = "#{@options[:email_prefix]}"
subject << "#{kontroller.controller_name}##{kontroller.action_name}" if kontroller
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/background_exception_notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BackgroundExceptionNotificationTest < ActiveSupport::TestCase
end

test "mail should have a descriptive subject" do
assert @mail.subject.include? "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\""
assert @mail.subject == "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\""
end

test "mail should say exception was raised in background" do
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PostsControllerTest < ActionController::TestCase
end

test "mail should have a descriptive subject" do
assert @mail.subject.include? "[Dummy ERROR] # (NoMethodError)"
assert @mail.subject.include? "[Dummy ERROR] # (NoMethodError) \"undefined method `nw'"
end

test "mail should contain backtrace in body" do
Expand Down
4 changes: 4 additions & 0 deletions test/exception_notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ class ExceptionNotificationTest < ActiveSupport::TestCase
test "should have default sections" do
assert ExceptionNotifier::Notifier.default_sections == %w(request session environment backtrace)
end

test "should have verbose subject by default" do
assert ExceptionNotifier::Notifier.default_options[:verbose_subject] == true
end
end

0 comments on commit 1c2af60

Please sign in to comment.