diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index a9672bce..f89fafce 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -76,7 +76,7 @@ def background_exception_notification(exception, options = {}, default_options = def compose_subject subject = @options[:email_prefix].to_s.dup subject << "(#{@options[:accumulated_errors_count]} times)" if @options[:accumulated_errors_count].to_i > 1 - subject << "#{@kontroller.controller_name} #{@kontroller.action_name}" if include_controller? + subject << "#{@kontroller.controller_name}##{@kontroller.action_name}" if include_controller? subject << " (#{@exception.class})" subject << " #{@exception.message.inspect}" if @options[:verbose_subject] subject = EmailNotifier.normalize_digits(subject) if @options[:normalize_subject] diff --git a/lib/exception_notifier/modules/formatter.rb b/lib/exception_notifier/modules/formatter.rb index ca358349..b4070421 100644 --- a/lib/exception_notifier/modules/formatter.rb +++ b/lib/exception_notifier/modules/formatter.rb @@ -111,7 +111,12 @@ def controller_and_action def rails_app_name return unless defined?(::Rails) && ::Rails.respond_to?(:application) - Rails.application.class.parent_name.underscore + + if Rails::VERSION::MAJOR >= 6 + Rails.application.class.module_parent_name.underscore + else + Rails.application.class.parent_name.underscore + end end def controller diff --git a/lib/exception_notifier/sns_notifier.rb b/lib/exception_notifier/sns_notifier.rb index 709cfde7..2da2f5b3 100644 --- a/lib/exception_notifier/sns_notifier.rb +++ b/lib/exception_notifier/sns_notifier.rb @@ -45,10 +45,12 @@ def build_message(exception, options) if options[:env].nil? text = "#{exception_name} occured in background\n" + data = options[:data] || {} else env = options[:env] kontroller = env['action_controller.instance'] + data = (env['exception_notifier.exception_data'] || {}).merge(options[:data] || {}) request = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>" text = "#{exception_name} occurred while #{request}" @@ -57,6 +59,7 @@ def build_message(exception, options) text += "Exception: #{exception.message}\n" text += "Hostname: #{Socket.gethostname}\n" + text += "Data: #{data}\n" return unless exception.backtrace diff --git a/test/exception_notifier/email_notifier_test.rb b/test/exception_notifier/email_notifier_test.rb index ccc732c5..ab6791e5 100644 --- a/test/exception_notifier/email_notifier_test.rb +++ b/test/exception_notifier/email_notifier_test.rb @@ -248,7 +248,7 @@ def index; end test 'sends mail with correct content' do assert_equal %("Dummy Notifier" ), @mail[:from].value assert_equal %w[dummyexceptions@example.com], @mail.to - assert_equal '[Dummy ERROR] home index (ZeroDivisionError) "divided by 0"', @mail.subject + assert_equal '[Dummy ERROR] home#index (ZeroDivisionError) "divided by 0"', @mail.subject assert_equal 'foobar', @mail['X-Custom-Header'].value assert_equal 'text/plain; charset=UTF-8', @mail.content_type assert_equal [], @mail.attachments diff --git a/test/exception_notifier/sns_notifier_test.rb b/test/exception_notifier/sns_notifier_test.rb index ce6ad0bc..cc7767f0 100644 --- a/test/exception_notifier/sns_notifier_test.rb +++ b/test/exception_notifier/sns_notifier_test.rb @@ -66,6 +66,7 @@ def setup message: "3 MyException occured in background\n" \ "Exception: undefined method 'method=' for Empty\n" \ "Hostname: example.com\n" \ + "Data: {}\n" \ "Backtrace:\n#{fake_backtrace.join("\n")}\n", subject: '[App Exception] - 3 MyException occurred' ) @@ -85,6 +86,7 @@ def setup "was processed by examples#index\n" \ "Exception: undefined method 'method=' for Empty\n" \ "Hostname: example.com\n" \ + "Data: {}\n" \ "Backtrace:\n#{fake_backtrace.join("\n")}\n", subject: '[App Exception] - A MyException occurred' ) @@ -98,6 +100,60 @@ def setup }) end + test 'should put data from env["exception_notifier.exception_data"] into text' do + controller = mock('controller') + controller.stubs(:action_name).returns('index') + controller.stubs(:controller_name).returns('examples') + + Aws::SNS::Client.any_instance.expects(:publish).with( + topic_arn: 'topicARN', + message: 'A MyException occurred while GET ' \ + "was processed by examples#index\n" \ + "Exception: undefined method 'method=' for Empty\n" \ + "Hostname: example.com\n" \ + "Data: {:current_user=>12}\n" \ + "Backtrace:\n#{fake_backtrace.join("\n")}\n", + subject: '[App Exception] - A MyException occurred' + ) + + sns_notifier = ExceptionNotifier::SnsNotifier.new(@options) + sns_notifier.call(@exception, + env: { + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/examples', + 'action_controller.instance' => controller, + 'exception_notifier.exception_data' => {current_user: 12} + }) + end + test 'should put optional data into text' do + controller = mock('controller') + controller.stubs(:action_name).returns('index') + controller.stubs(:controller_name).returns('examples') + + Aws::SNS::Client.any_instance.expects(:publish).with( + topic_arn: 'topicARN', + message: 'A MyException occurred while GET ' \ + "was processed by examples#index\n" \ + "Exception: undefined method 'method=' for Empty\n" \ + "Hostname: example.com\n" \ + "Data: {:current_user=>12}\n" \ + "Backtrace:\n#{fake_backtrace.join("\n")}\n", + subject: '[App Exception] - A MyException occurred' + ) + + sns_notifier = ExceptionNotifier::SnsNotifier.new(@options) + sns_notifier.call(@exception, + env: { + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/examples', + 'action_controller.instance' => controller, + }, + data: { + current_user: 12 + } + ) + end + private def fake_exception