diff --git a/README.md b/README.md index a0bf6011..91f2fbc0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/exception_notifier/notifier.rb b/lib/exception_notifier/notifier.rb index 5d7bb48e..f1c633e9 100644 --- a/lib/exception_notifier/notifier.rb +++ b/lib/exception_notifier/notifier.rb @@ -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 @@ -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" } @@ -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" } @@ -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) diff --git a/test/dummy/Gemfile.lock b/test/dummy/Gemfile.lock index 63e9badb..944ea15c 100644 --- a/test/dummy/Gemfile.lock +++ b/test/dummy/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../../.. specs: - exception_notification (2.4.1) + exception_notification (2.5.2) actionmailer (>= 3.0.4) GEM diff --git a/test/dummy/test/functional/posts_controller_test.rb b/test/dummy/test/functional/posts_controller_test.rb index 1357a83e..b39842a5 100644 --- a/test/dummy/test/functional/posts_controller_test.rb +++ b/test/dummy/test/functional/posts_controller_test.rb @@ -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