Skip to content

Commit

Permalink
Merge pull request #1 from smartinez87/master
Browse files Browse the repository at this point in the history
Merging fork updates
  • Loading branch information
cdadityang committed Jan 26, 2021
2 parents cdec24e + e9f09b8 commit 1efde75
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/exception_notifier/email_notifier.rb
Expand Up @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion lib/exception_notifier/modules/formatter.rb
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/exception_notifier/sns_notifier.rb
Expand Up @@ -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}"
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/exception_notifier/email_notifier_test.rb
Expand Up @@ -248,7 +248,7 @@ def index; end
test 'sends mail with correct content' do
assert_equal %("Dummy Notifier" <dummynotifier@example.com>), @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
Expand Down
56 changes: 56 additions & 0 deletions test/exception_notifier/sns_notifier_test.rb
Expand Up @@ -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'
)
Expand All @@ -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'
)
Expand All @@ -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 </examples> ' \
"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 </examples> ' \
"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
Expand Down

0 comments on commit 1efde75

Please sign in to comment.