Skip to content

Commit

Permalink
Merge d889586 into 05ba3ab
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioCristalli committed Apr 19, 2019
2 parents 05ba3ab + d889586 commit d6bc340
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/exception_notification/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(app, options = {})

if options.key?(:error_grouping_cache)
ExceptionNotifier.error_grouping_cache = options.delete(:error_grouping_cache)
elsif defined?(Rails)
elsif defined?(Rails) && Rails.respond_to?(:cache)
ExceptionNotifier.error_grouping_cache = Rails.cache
end

Expand Down
2 changes: 2 additions & 0 deletions lib/exception_notifier/datadog_notifier.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'action_dispatch'

module ExceptionNotifier
class DatadogNotifier < BaseNotifier
attr_reader :client,
Expand Down
13 changes: 11 additions & 2 deletions lib/exception_notifier/teams_notifier.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'action_dispatch'
require 'active_support/core_ext/time'
require 'json'

module ExceptionNotifier
class TeamsNotifier < BaseNotifier
Expand All @@ -24,7 +25,7 @@ def call(exception, options = {})

@env = @options.delete(:env)

@application_name = @options.delete(:app_name) || Rails.application.class.parent_name.underscore
@application_name = @options.delete(:app_name) || rails_app_name
@gitlab_url = @options.delete(:git_url)
@jira_url = @options.delete(:jira_url)

Expand Down Expand Up @@ -69,7 +70,7 @@ def message_text
'@type' => 'MessageCard',
'@context' => 'http://schema.org/extensions',
'summary' => "#{@application_name} Exception Alert",
'title' => "⚠️ Exception Occurred in #{Rails.env} ⚠️",
'title' => "⚠️ Exception Occurred in #{env_name} ⚠️",
'sections' => [
{
'activityTitle' => "#{errors_count > 1 ? errors_count : 'A'} *#{@exception.class}* occurred" + (@controller ? " in *#{controller_and_method}*." : '.'),
Expand Down Expand Up @@ -174,5 +175,13 @@ def hash_presentation(hash)

text.join(" \n")
end

def rails_app_name
Rails.application.class.parent_name.underscore if defined?(Rails) && Rails.respond_to?(:application)
end

def env_name
Rails.env if defined?(Rails) && Rails.respond_to?(:env)
end
end
end
4 changes: 0 additions & 4 deletions test/dummy/.gitignore

This file was deleted.

Empty file removed test/dummy/config.ru
Empty file.
32 changes: 0 additions & 32 deletions test/dummy/config/application.rb

This file was deleted.

8 changes: 5 additions & 3 deletions test/exception_notification/rack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class RackTest < ActiveSupport::TestCase
assert_respond_to ExceptionNotifier.notification_trigger, :call
end

test 'should set default cache to Rails cache' do
ExceptionNotification::Rack.new(@normal_app, error_grouping: true).call({})
assert_equal Rails.cache, ExceptionNotifier.error_grouping_cache
if defined?(Rails) && Rails.respond_to?(:cache)
test 'should set default cache to Rails cache' do
ExceptionNotification::Rack.new(@normal_app, error_grouping: true).call({})
assert_equal Rails.cache, ExceptionNotifier.error_grouping_cache
end
end

test 'should ignore exceptions with Usar Agent in ignore_crawlers' do
Expand Down
17 changes: 13 additions & 4 deletions test/exception_notifier/email_notifier_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test_helper'
require 'action_mailer'
require 'action_controller'

class EmailNotifierTest < ActiveSupport::TestCase
setup do
Expand All @@ -23,6 +24,8 @@ class EmailNotifierTest < ActiveSupport::TestCase
}
)

@email_notifier.mailer.append_view_path "#{File.dirname(__FILE__)}/../support/views"

@mail = @email_notifier.call(
@exception,
data: { job: 'DivideWorkerJob', payload: '1/0', message: 'My Custom Message' }
Expand All @@ -44,7 +47,7 @@ class EmailNotifierTest < ActiveSupport::TestCase
assert_equal 'Dummy user_name', @mail.delivery_method.settings[:user_name]
assert_equal 'Dummy password', @mail.delivery_method.settings[:password]

body = <<-BODY.strip_heredoc
body = <<-BODY.gsub(/^ /, '')
A ZeroDivisionError occurred in background at Sat, 20 Apr 2013 20:58:55 UTC +00:00 :
divided by 0
Expand Down Expand Up @@ -244,6 +247,8 @@ def index; end
post_callback: proc { |_opts, _notifier, _backtrace, _message, message_opts| message_opts[:post_callback_called] = 1 }
)

@email_notifier.mailer.append_view_path "#{File.dirname(__FILE__)}/../support/views"

@controller = HomeController.new
@controller.process(:index)

Expand All @@ -269,7 +274,7 @@ def index; end
assert_equal 'text/plain; charset=UTF-8', @mail.content_type
assert_equal [], @mail.attachments

body = <<-BODY.strip_heredoc
body = <<-BODY.gsub(/^ /, '')
A ZeroDivisionError occurred in home#index:
divided by 0
Expand All @@ -292,8 +297,12 @@ def index; end
* Parameters : {\"id\"=>\"foo\", \"secret\"=>\"[FILTERED]\"}
* Timestamp : Sat, 20 Apr 2013 20:58:55 UTC +00:00
* Server : #{Socket.gethostname}
* Rails root : #{Rails.root}
* Process: #{$PROCESS_ID}
BODY

body << " * Rails root : #{Rails.root}\n" if defined?(Rails) && Rails.respond_to?(:root)

body << <<-BODY.gsub(/^ /, '')
* Process: #{Process.pid}
-------------------------------
Session:
Expand Down
16 changes: 13 additions & 3 deletions test/exception_notifier/google_chat_notifier_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'test_helper'
require 'rack'
require 'httparty'
require 'timecop'
require 'json'

class GoogleChatNotifierTest < ActiveSupport::TestCase
URL = 'http://localhost:8000'.freeze
Expand All @@ -21,7 +23,7 @@ def teardown
test 'shoud use errors count if accumulated_errors_count is provided' do
text = [
'',
'Application: *dummy*',
"Application: *#{app_name}*",
'5 *ArgumentError* occurred.',
'',
body
Expand Down Expand Up @@ -159,13 +161,21 @@ def test_env
def header
[
'',
'Application: *dummy*',
"Application: *#{app_name}*",
'An *ArgumentError* occurred.',
''
].join("\n")
end

def body
"⚠️ Error occurred in test ⚠️\n*foo*"
if defined?(::Rails) && ::Rails.respond_to?(:env)
"⚠️ Error occurred in test ⚠️\n*foo*"
else
"⚠️ Error occurred ⚠️\n*foo*"
end
end

def app_name
'dummy' if defined?(::Rails) && ::Rails.respond_to?(:application)
end
end
1 change: 1 addition & 0 deletions test/exception_notifier/hipchat_notifier_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'rack'

# silence_warnings trick around require can be removed once
# https://github.com/hipchat/hipchat-rb/pull/174
Expand Down
28 changes: 23 additions & 5 deletions test/exception_notifier/mattermost_notifier_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'
require 'httparty'
require 'timecop'
require 'json'

class MattermostNotifierTest < ActiveSupport::TestCase
URL = 'http://localhost:8000'.freeze
Expand All @@ -27,10 +28,10 @@ def teardown
body = default_body.merge(
text: [
'@channel',
'### ⚠️ Error occurred in test ⚠️',
error_occurred_in,
'An *ArgumentError* occurred.',
'*foo*',
'[Create an issue](github.com/aschen/dummy/issues/new/?issue%5Btitle%5D=%5BBUG%5D+Error+500+%3A++%28ArgumentError%29+foo)'
github_link
].join("\n")
)

Expand Down Expand Up @@ -96,7 +97,7 @@ def teardown
body = default_body.merge(
text: [
'@channel',
'### ⚠️ Error occurred in test ⚠️',
error_occurred_in,
'5 *ArgumentError* occurred.',
'*foo*'
].join("\n")
Expand All @@ -115,7 +116,7 @@ def teardown
body = default_body.merge(
text: [
'@channel',
'### ⚠️ Error occurred in test ⚠️',
error_occurred_in,
'An *ArgumentError* occurred.',
'*foo*',
'### Request',
Expand Down Expand Up @@ -160,7 +161,7 @@ def default_body
{
text: [
'@channel',
'### ⚠️ Error occurred in test ⚠️',
error_occurred_in,
'An *ArgumentError* occurred.',
'*foo*'
].join("\n"),
Expand All @@ -181,4 +182,21 @@ def test_env
params: { id: 'foo' }
)
end

def error_occurred_in
if defined?(::Rails) && ::Rails.respond_to?(:env)
'### ⚠️ Error occurred in test ⚠️'
else
'### ⚠️ Error occurred ⚠️'
end
end

def github_link
if defined?(::Rails) && ::Rails.respond_to?(:application)
'[Create an issue](github.com/aschen/dummy/issues/new/?issue%5Btitle%5D=%5BBUG%5D+Error+500+%3A++%28ArgumentError%29+foo)'
else
# TODO: fix missing app name
'[Create an issue](github.com/aschen//issues/new/?issue%5Btitle%5D=%5BBUG%5D+Error+500+%3A++%28ArgumentError%29+foo)'
end
end
end
38 changes: 24 additions & 14 deletions test/exception_notifier/modules/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
require 'timecop'

class FormatterTest < ActiveSupport::TestCase
class HomeController < ActionController::Metal
def index; end
end

setup do
@exception = RuntimeError.new('test')
Timecop.freeze('2018-12-09 12:07:16 UTC')
Expand All @@ -20,7 +16,14 @@ def index; end
#
test 'title returns correct content' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal '⚠️ Error occurred in test ⚠️', formatter.title

title = if defined?(::Rails) && ::Rails.respond_to?(:env)
'⚠️ Error occurred in test ⚠️'
else
'⚠️ Error occurred ⚠️'
end

assert_equal title, formatter.title
end

#
Expand All @@ -37,11 +40,8 @@ def index; end
end

test 'subtitle with controller' do
controller = HomeController.new
controller.process(:index)

env = Rack::MockRequest.env_for(
'/', 'action_controller.instance' => controller
'/', 'action_controller.instance' => test_controller
)

formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
Expand All @@ -53,7 +53,12 @@ def index; end
#
test 'app_name defaults to Rails app name' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal 'dummy', formatter.app_name

if defined?(::Rails) && ::Rails.respond_to?(:application)
assert_equal 'dummy', formatter.app_name
else
assert_nil formatter.app_name
end
end

test 'app_name can be overwritten using options' do
Expand Down Expand Up @@ -120,11 +125,8 @@ def index; end
# #controller_and_action
#
test 'correct controller_and_action if controller is present' do
controller = HomeController.new
controller.process(:index)

env = Rack::MockRequest.env_for(
'/', 'action_controller.instance' => controller
'/', 'action_controller.instance' => test_controller
)

formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
Expand All @@ -137,4 +139,12 @@ def index; end
formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
assert_nil formatter.controller_and_action
end

def test_controller
controller = mock('controller')
controller.stubs(:action_name).returns('index')
controller.stubs(:controller_name).returns('home')

controller
end
end
8 changes: 4 additions & 4 deletions test/exception_notifier/sns_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def setup
end

test 'should send a sns notification with controller#action information' do
ExamplesController.any_instance.stubs(:action_name).returns('index')
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',
Expand All @@ -90,14 +92,12 @@ def setup
env: {
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/examples',
'action_controller.instance' => ExamplesController.new
'action_controller.instance' => controller
})
end

private

class ExamplesController < ActionController::Base; end

def fake_exception
1 / 0
rescue StandardError => e
Expand Down

0 comments on commit d6bc340

Please sign in to comment.