exception_notification 3.0.1 → 4.0.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/.travis.yml +9 -0
- data/Appraisals +11 -0
- data/CHANGELOG.rdoc +21 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +49 -7
- data/README.md +417 -184
- data/Rakefile +4 -2
- data/examples/sinatra/Gemfile +8 -0
- data/examples/sinatra/Gemfile.lock +95 -0
- data/examples/sinatra/Procfile +2 -0
- data/examples/sinatra/README.md +11 -0
- data/examples/sinatra/config.ru +3 -0
- data/examples/sinatra/sinatra_app.rb +28 -0
- data/exception_notification.gemspec +10 -4
- data/gemfiles/rails3_1.gemfile +7 -0
- data/gemfiles/rails3_2.gemfile +7 -0
- data/gemfiles/rails4_0.gemfile +7 -0
- data/lib/exception_notification.rb +10 -0
- data/lib/exception_notification/rack.rb +45 -0
- data/lib/exception_notification/rails.rb +8 -0
- data/lib/exception_notification/resque.rb +24 -0
- data/lib/exception_notification/sidekiq.rb +22 -0
- data/lib/exception_notifier.rb +89 -61
- data/lib/exception_notifier/campfire_notifier.rb +2 -7
- data/lib/exception_notifier/email_notifier.rb +181 -0
- data/lib/exception_notifier/notifier.rb +9 -178
- data/lib/exception_notifier/views/exception_notifier/_backtrace.html.erb +3 -1
- data/lib/exception_notifier/views/exception_notifier/_data.html.erb +6 -1
- data/lib/exception_notifier/views/exception_notifier/_environment.html.erb +16 -6
- data/lib/exception_notifier/views/exception_notifier/_environment.text.erb +1 -1
- data/lib/exception_notifier/views/exception_notifier/_request.html.erb +24 -5
- data/lib/exception_notifier/views/exception_notifier/_request.text.erb +2 -0
- data/lib/exception_notifier/views/exception_notifier/_session.html.erb +10 -2
- data/lib/exception_notifier/views/exception_notifier/_session.text.erb +1 -1
- data/lib/exception_notifier/views/exception_notifier/_title.html.erb +3 -3
- data/lib/exception_notifier/views/exception_notifier/background_exception_notification.html.erb +38 -11
- data/lib/exception_notifier/views/exception_notifier/background_exception_notification.text.erb +0 -1
- data/lib/exception_notifier/views/exception_notifier/exception_notification.html.erb +39 -21
- data/lib/exception_notifier/views/exception_notifier/exception_notification.text.erb +0 -1
- data/lib/exception_notifier/webhook_notifier.rb +21 -0
- data/lib/generators/exception_notification/install_generator.rb +15 -0
- data/lib/generators/exception_notification/templates/exception_notification.rb +47 -0
- data/test/dummy/Gemfile +2 -1
- data/test/dummy/Gemfile.lock +79 -78
- data/test/dummy/config/environment.rb +9 -7
- data/test/dummy/test/functional/posts_controller_test.rb +22 -37
- data/test/{campfire_test.rb → exception_notifier/campfire_notifier_test.rb} +4 -4
- data/test/exception_notifier/email_notifier_test.rb +144 -0
- data/test/exception_notifier/webhook_notifier_test.rb +41 -0
- data/test/exception_notifier_test.rb +101 -0
- data/test/test_helper.rb +4 -1
- metadata +136 -18
- data/test/background_exception_notification_test.rb +0 -82
- data/test/exception_notification_test.rb +0 -73
@@ -1,13 +1,15 @@
|
|
1
1
|
# Load the rails application
|
2
2
|
require File.expand_path('../application', __FILE__)
|
3
3
|
|
4
|
-
Dummy::Application.config.middleware.use
|
5
|
-
:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
Dummy::Application.config.middleware.use ExceptionNotification::Rack,
|
5
|
+
:email => {
|
6
|
+
:email_prefix => "[Dummy ERROR] ",
|
7
|
+
:sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
|
8
|
+
:exception_recipients => %w{dummyexceptions@example.com},
|
9
|
+
:email_headers => { "X-Custom-Header" => "foobar" },
|
10
|
+
:sections => ['new_section', 'request', 'session', 'environment', 'backtrace'],
|
11
|
+
:background_sections => %w(new_bkg_section backtrace data)
|
12
|
+
}
|
11
13
|
|
12
14
|
# Initialize the rails application
|
13
15
|
Dummy::Application.initialize!
|
@@ -2,12 +2,14 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class PostsControllerTest < ActionController::TestCase
|
4
4
|
setup do
|
5
|
+
Time.stubs(:current).returns('Sat, 20 Apr 2013 20:58:55 UTC +00:00')
|
6
|
+
@email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
|
5
7
|
begin
|
6
8
|
@post = posts(:one)
|
7
9
|
post :create, :post => @post.attributes
|
8
10
|
rescue => e
|
9
11
|
@exception = e
|
10
|
-
@mail =
|
12
|
+
@mail = @email_notifier.create_email(@exception, {:env => request.env, :data => {:message => 'My Custom Message'}})
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -72,8 +74,8 @@ class PostsControllerTest < ActionController::TestCase
|
|
72
74
|
get :show, :id => @post.to_param + "10"
|
73
75
|
rescue => e
|
74
76
|
@ignored_exception = e
|
75
|
-
unless ExceptionNotifier.
|
76
|
-
@ignored_mail =
|
77
|
+
unless ExceptionNotifier.ignored_exceptions.include?(@ignored_exception.class.name)
|
78
|
+
@ignored_mail = @email_notifier.create_email(@ignored_exception, {:env => request.env})
|
77
79
|
end
|
78
80
|
end
|
79
81
|
|
@@ -87,14 +89,14 @@ class PostsControllerTest < ActionController::TestCase
|
|
87
89
|
@post = posts(:one)
|
88
90
|
post :create, :post => @post.attributes
|
89
91
|
rescue => e
|
90
|
-
@secured_mail =
|
92
|
+
@secured_mail = @email_notifier.create_email(e, {:env => request.env})
|
91
93
|
end
|
92
94
|
|
93
95
|
assert request.ssl?
|
94
96
|
assert @secured_mail.encoded.include? "* session id: [FILTERED]\r\n *"
|
95
97
|
end
|
96
98
|
|
97
|
-
test "should ignore exception if from unwanted
|
99
|
+
test "should ignore exception if from unwanted crawler" do
|
98
100
|
request.env['HTTP_USER_AGENT'] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
|
99
101
|
begin
|
100
102
|
@post = posts(:one)
|
@@ -105,27 +107,8 @@ class PostsControllerTest < ActionController::TestCase
|
|
105
107
|
custom_env['exception_notifier.options'] ||= {}
|
106
108
|
custom_env['exception_notifier.options'].merge!(:ignore_crawlers => %w(Googlebot))
|
107
109
|
ignore_array = custom_env['exception_notifier.options'][:ignore_crawlers]
|
108
|
-
unless
|
109
|
-
@ignored_mail =
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
assert_nil @ignored_mail
|
114
|
-
end
|
115
|
-
|
116
|
-
test "should ignore exception if satisfies conditional ignore" do
|
117
|
-
request.env['IGNOREME'] = "IGNOREME"
|
118
|
-
begin
|
119
|
-
@post = posts(:one)
|
120
|
-
post :create, :post => @post.attributes
|
121
|
-
rescue => e
|
122
|
-
@exception = e
|
123
|
-
custom_env = request.env
|
124
|
-
custom_env['exception_notifier.options'] ||= {}
|
125
|
-
ignore_cond = {:ignore_if => lambda {|env, e| (env['IGNOREME'] == 'IGNOREME') && (e.message =~ /undefined method/)}}
|
126
|
-
custom_env['exception_notifier.options'].merge!(ignore_cond)
|
127
|
-
unless ExceptionNotifier.new(Dummy::Application, custom_env['exception_notifier.options']).send(:conditionally_ignored, ignore_cond[:ignore_if], custom_env, @exception)
|
128
|
-
@ignored_mail = ExceptionNotifier::Notifier.exception_notification(custom_env, @exception)
|
110
|
+
unless ExceptionNotification::Rack.new(Dummy::Application, custom_env['exception_notifier.options']).send(:from_crawler, custom_env, ignore_array)
|
111
|
+
@ignored_mail = @email_notifier.create_email(@exception, {:env => custom_env})
|
129
112
|
end
|
130
113
|
end
|
131
114
|
|
@@ -141,7 +124,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
141
124
|
custom_env = request.env
|
142
125
|
custom_env['exception_notifier.options'] ||= {}
|
143
126
|
custom_env['exception_notifier.options'].merge!({:email_format => :html})
|
144
|
-
@mail =
|
127
|
+
@mail = @email_notifier.create_email(@exception, {:env => custom_env})
|
145
128
|
end
|
146
129
|
|
147
130
|
assert @mail.content_type.include? "multipart/alternative"
|
@@ -151,13 +134,13 @@ end
|
|
151
134
|
class PostsControllerTestWithoutVerboseSubject < ActionController::TestCase
|
152
135
|
tests PostsController
|
153
136
|
setup do
|
154
|
-
ExceptionNotifier::
|
137
|
+
@email_notifier = ExceptionNotifier::EmailNotifier.new(:verbose_subject => false)
|
155
138
|
begin
|
156
139
|
@post = posts(:one)
|
157
140
|
post :create, :post => @post.attributes
|
158
141
|
rescue => e
|
159
142
|
@exception = e
|
160
|
-
@mail =
|
143
|
+
@mail = @email_notifier.create_email(@exception, {:env => request.env})
|
161
144
|
end
|
162
145
|
end
|
163
146
|
|
@@ -169,17 +152,17 @@ end
|
|
169
152
|
class PostsControllerTestWithSmtpSettings < ActionController::TestCase
|
170
153
|
tests PostsController
|
171
154
|
setup do
|
172
|
-
ExceptionNotifier::
|
155
|
+
@email_notifier = ExceptionNotifier::EmailNotifier.new(:smtp_settings => {
|
173
156
|
:user_name => "Dummy user_name",
|
174
157
|
:password => "Dummy password"
|
175
|
-
}
|
176
|
-
|
158
|
+
})
|
159
|
+
|
177
160
|
begin
|
178
161
|
@post = posts(:one)
|
179
162
|
post :create, :post => @post.attributes
|
180
163
|
rescue => e
|
181
164
|
@exception = e
|
182
|
-
@mail =
|
165
|
+
@mail = @email_notifier.create_email(@exception, {:env => request.env})
|
183
166
|
end
|
184
167
|
end
|
185
168
|
|
@@ -187,9 +170,9 @@ class PostsControllerTestWithSmtpSettings < ActionController::TestCase
|
|
187
170
|
assert_equal "Dummy user_name", @mail.delivery_method.settings[:user_name]
|
188
171
|
assert_equal "Dummy password", @mail.delivery_method.settings[:password]
|
189
172
|
end
|
190
|
-
|
173
|
+
|
191
174
|
test "should have overridden smtp settings with background notification" do
|
192
|
-
@mail =
|
175
|
+
@mail = @email_notifier.create_email(@exception)
|
193
176
|
assert_equal "Dummy user_name", @mail.delivery_method.settings[:user_name]
|
194
177
|
assert_equal "Dummy password", @mail.delivery_method.settings[:password]
|
195
178
|
end
|
@@ -198,6 +181,7 @@ end
|
|
198
181
|
class PostsControllerTestBadRequestData < ActionController::TestCase
|
199
182
|
tests PostsController
|
200
183
|
setup do
|
184
|
+
@email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
|
201
185
|
begin
|
202
186
|
# This might seem synthetic, but the point is that the data used by
|
203
187
|
# ExceptionNotification could be rendered "invalid" by e.g. a badly
|
@@ -213,7 +197,7 @@ class PostsControllerTestBadRequestData < ActionController::TestCase
|
|
213
197
|
post :create, :post => @post.attributes
|
214
198
|
rescue => e
|
215
199
|
@exception = e
|
216
|
-
@mail =
|
200
|
+
@mail = @email_notifier.create_email(@exception, {:env => request.env})
|
217
201
|
end
|
218
202
|
end
|
219
203
|
|
@@ -225,11 +209,12 @@ end
|
|
225
209
|
class PostsControllerTestBackgroundNotification < ActionController::TestCase
|
226
210
|
tests PostsController
|
227
211
|
setup do
|
212
|
+
@email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
|
228
213
|
begin
|
229
214
|
@post = posts(:one)
|
230
215
|
post :create, :post => @post.attributes
|
231
216
|
rescue => exception
|
232
|
-
@mail =
|
217
|
+
@mail = @email_notifier.create_email(exception)
|
233
218
|
end
|
234
219
|
end
|
235
220
|
|
@@ -6,8 +6,8 @@ class CampfireNotifierTest < ActiveSupport::TestCase
|
|
6
6
|
test "should send campfire notification if properly configured" do
|
7
7
|
ExceptionNotifier::CampfireNotifier.stubs(:new).returns(Object.new)
|
8
8
|
campfire = ExceptionNotifier::CampfireNotifier.new({:subdomain => 'test', :token => 'test_token', :room_name => 'test_room'})
|
9
|
-
campfire.stubs(:
|
10
|
-
notif = campfire.
|
9
|
+
campfire.stubs(:call).returns(fake_notification)
|
10
|
+
notif = campfire.call(fake_exception)
|
11
11
|
|
12
12
|
assert !notif[:message].empty?
|
13
13
|
assert_equal notif[:message][:type], 'PasteMessage'
|
@@ -22,7 +22,7 @@ class CampfireNotifierTest < ActiveSupport::TestCase
|
|
22
22
|
campfire = ExceptionNotifier::CampfireNotifier.new(wrong_params)
|
23
23
|
|
24
24
|
assert_nil campfire.room
|
25
|
-
assert_nil campfire.
|
25
|
+
assert_nil campfire.call(fake_exception)
|
26
26
|
end
|
27
27
|
|
28
28
|
test "should not send campfire notification if config attr missing" do
|
@@ -31,7 +31,7 @@ class CampfireNotifierTest < ActiveSupport::TestCase
|
|
31
31
|
campfire = ExceptionNotifier::CampfireNotifier.new(wrong_params)
|
32
32
|
|
33
33
|
assert_nil campfire.room
|
34
|
-
assert_nil campfire.
|
34
|
+
assert_nil campfire.call(fake_exception)
|
35
35
|
end
|
36
36
|
|
37
37
|
private
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class EmailNotifierTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
Time.stubs(:current).returns('Sat, 20 Apr 2013 20:58:55 UTC +00:00')
|
6
|
+
@email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
|
7
|
+
begin
|
8
|
+
1/0
|
9
|
+
rescue => e
|
10
|
+
@exception = e
|
11
|
+
@mail = @email_notifier.create_email(@exception,
|
12
|
+
:data => {:job => 'DivideWorkerJob', :payload => '1/0', :message => 'My Custom Message'})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should have default sender address overridden" do
|
17
|
+
assert @email_notifier.sender_address == %("Dummy Notifier" <dummynotifier@example.com>)
|
18
|
+
end
|
19
|
+
|
20
|
+
test "should have default exception recipients overridden" do
|
21
|
+
assert @email_notifier.exception_recipients == %w(dummyexceptions@example.com)
|
22
|
+
end
|
23
|
+
|
24
|
+
test "should have default email prefix overridden" do
|
25
|
+
assert @email_notifier.email_prefix == "[Dummy ERROR] "
|
26
|
+
end
|
27
|
+
|
28
|
+
test "should have default email headers overridden" do
|
29
|
+
assert @email_notifier.email_headers == { "X-Custom-Header" => "foobar"}
|
30
|
+
end
|
31
|
+
|
32
|
+
test "should have default sections overridden" do
|
33
|
+
for section in %w(new_section request session environment backtrace)
|
34
|
+
assert @email_notifier.sections.include? section
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should have default background sections" do
|
39
|
+
for section in %w(new_bkg_section backtrace data)
|
40
|
+
assert @email_notifier.background_sections.include? section
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
test "should have email format by default" do
|
45
|
+
assert @email_notifier.email_format == :text
|
46
|
+
end
|
47
|
+
|
48
|
+
test "should have verbose subject by default" do
|
49
|
+
assert @email_notifier.verbose_subject == true
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should have normalize_subject false by default" do
|
53
|
+
assert @email_notifier.normalize_subject == false
|
54
|
+
end
|
55
|
+
|
56
|
+
test "should have delivery_method nil by default" do
|
57
|
+
assert @email_notifier.delivery_method == nil
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should have mailer_settings nil by default" do
|
61
|
+
assert @email_notifier.mailer_settings == nil
|
62
|
+
end
|
63
|
+
|
64
|
+
test "should have mailer_parent by default" do
|
65
|
+
assert @email_notifier.mailer_parent == 'ActionMailer::Base'
|
66
|
+
end
|
67
|
+
|
68
|
+
test "should have template_path by default" do
|
69
|
+
assert @email_notifier.template_path == 'exception_notifier'
|
70
|
+
end
|
71
|
+
|
72
|
+
test "should normalize multiple digits into one N" do
|
73
|
+
assert_equal 'N foo N bar N baz N',
|
74
|
+
ExceptionNotifier::EmailNotifier.normalize_digits('1 foo 12 bar 123 baz 1234')
|
75
|
+
end
|
76
|
+
|
77
|
+
test "mail should be plain text and UTF-8 enconded by default" do
|
78
|
+
assert @mail.content_type == "text/plain; charset=UTF-8"
|
79
|
+
end
|
80
|
+
|
81
|
+
test "should have raised an exception" do
|
82
|
+
assert_not_nil @exception
|
83
|
+
end
|
84
|
+
|
85
|
+
test "should have generated a notification email" do
|
86
|
+
assert_not_nil @mail
|
87
|
+
end
|
88
|
+
|
89
|
+
test "mail should have a from address set" do
|
90
|
+
assert @mail.from == ["dummynotifier@example.com"]
|
91
|
+
end
|
92
|
+
|
93
|
+
test "mail should have a to address set" do
|
94
|
+
assert @mail.to == ["dummyexceptions@example.com"]
|
95
|
+
end
|
96
|
+
|
97
|
+
test "mail should have a descriptive subject" do
|
98
|
+
assert @mail.subject == "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\""
|
99
|
+
end
|
100
|
+
|
101
|
+
test "mail should say exception was raised in background at show timestamp" do
|
102
|
+
assert @mail.encoded.include? "A ZeroDivisionError occurred in background at #{Time.current}"
|
103
|
+
end
|
104
|
+
|
105
|
+
test "mail should prefix exception class with 'an' instead of 'a' when it starts with a vowel" do
|
106
|
+
begin
|
107
|
+
raise ActiveRecord::RecordNotFound
|
108
|
+
rescue => e
|
109
|
+
@vowel_exception = e
|
110
|
+
@vowel_mail = @email_notifier.create_email(@vowel_exception)
|
111
|
+
end
|
112
|
+
|
113
|
+
assert @vowel_mail.encoded.include? "An ActiveRecord::RecordNotFound occurred in background at #{Time.current}"
|
114
|
+
end
|
115
|
+
|
116
|
+
test "mail should contain backtrace in body" do
|
117
|
+
assert @mail.encoded.include?("test/exception_notifier/email_notifier_test.rb:8"), "\n#{@mail.inspect}"
|
118
|
+
end
|
119
|
+
|
120
|
+
test "mail should contain data in body" do
|
121
|
+
assert @mail.encoded.include? '* data:'
|
122
|
+
assert @mail.encoded.include? ':payload=>"1/0"'
|
123
|
+
assert @mail.encoded.include? ':job=>"DivideWorkerJob"'
|
124
|
+
assert @mail.encoded.include? "My Custom Message"
|
125
|
+
end
|
126
|
+
|
127
|
+
test "mail should not contain any attachments" do
|
128
|
+
assert @mail.attachments == []
|
129
|
+
end
|
130
|
+
|
131
|
+
test "should not send notification if one of ignored exceptions" do
|
132
|
+
begin
|
133
|
+
raise ActiveRecord::RecordNotFound
|
134
|
+
rescue => e
|
135
|
+
@ignored_exception = e
|
136
|
+
unless ExceptionNotifier.ignored_exceptions.include?(@ignored_exception.class.name)
|
137
|
+
@ignored_mail = @email_notifier.create_email(@ignored_exception)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
assert @ignored_exception.class.inspect == "ActiveRecord::RecordNotFound"
|
142
|
+
assert_nil @ignored_mail
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
class WebhookNotifierTest < ActiveSupport::TestCase
|
5
|
+
|
6
|
+
test "should send webhook notification if properly configured" do
|
7
|
+
ExceptionNotifier::WebhookNotifier.stubs(:new).returns(Object.new)
|
8
|
+
webhook = ExceptionNotifier::WebhookNotifier.new({:url => 'http://localhost:8000'})
|
9
|
+
webhook.stubs(:call).returns(fake_response)
|
10
|
+
response = webhook.call(fake_exception)
|
11
|
+
|
12
|
+
assert_not_nil response
|
13
|
+
assert_equal response[:status], 200
|
14
|
+
assert_equal response[:body][:exception][:error_class], "ZeroDivisionError"
|
15
|
+
assert response[:body][:exception][:message].include? "divided by 0"
|
16
|
+
assert response[:body][:exception][:backtrace].include? "/exception_notification/test/webhook_notifier_test.rb:48"
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def fake_response
|
22
|
+
{
|
23
|
+
:status => 200,
|
24
|
+
:body => {
|
25
|
+
:exception => {
|
26
|
+
:error_class => 'ZeroDivisionError',
|
27
|
+
:message => 'divided by 0',
|
28
|
+
:backtrace => '/exception_notification/test/webhook_notifier_test.rb:48:in `/'
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def fake_exception
|
35
|
+
exception = begin
|
36
|
+
5/0
|
37
|
+
rescue Exception => e
|
38
|
+
e
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ExceptionNotifierTest < ActiveSupport::TestCase
|
4
|
+
test "should have default ignored exceptions" do
|
5
|
+
assert ExceptionNotifier.ignored_exceptions == ['ActiveRecord::RecordNotFound', 'AbstractController::ActionNotFound', 'ActionController::RoutingError']
|
6
|
+
end
|
7
|
+
|
8
|
+
test "should have email notifier registered" do
|
9
|
+
assert ExceptionNotifier.notifiers == [:email]
|
10
|
+
end
|
11
|
+
|
12
|
+
test "should have a valid email notifier" do
|
13
|
+
@email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
|
14
|
+
assert_not_nil @email_notifier
|
15
|
+
assert @email_notifier.class == ExceptionNotifier::EmailNotifier
|
16
|
+
assert @email_notifier.respond_to?(:call)
|
17
|
+
end
|
18
|
+
|
19
|
+
test "should allow register/unregister another notifier" do
|
20
|
+
called = false
|
21
|
+
proc_notifier = lambda { |exception, options| called = true }
|
22
|
+
ExceptionNotifier.register_exception_notifier(:proc, proc_notifier)
|
23
|
+
|
24
|
+
assert ExceptionNotifier.notifiers.sort == [:email, :proc]
|
25
|
+
|
26
|
+
exception = StandardError.new
|
27
|
+
ExceptionNotifier.notify_exception(exception)
|
28
|
+
assert called == true
|
29
|
+
|
30
|
+
ExceptionNotifier.unregister_exception_notifier(:proc)
|
31
|
+
assert ExceptionNotifier.notifiers == [:email]
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should allow select notifiers to send error to" do
|
35
|
+
notifier1_calls = 0
|
36
|
+
notifier1 = lambda { |exception, options| notifier1_calls += 1 }
|
37
|
+
ExceptionNotifier.register_exception_notifier(:notifier1, notifier1)
|
38
|
+
|
39
|
+
notifier2_calls = 0
|
40
|
+
notifier2 = lambda { |exception, options| notifier2_calls += 1 }
|
41
|
+
ExceptionNotifier.register_exception_notifier(:notifier2, notifier2)
|
42
|
+
|
43
|
+
assert ExceptionNotifier.notifiers.sort == [:email, :notifier1, :notifier2]
|
44
|
+
|
45
|
+
exception = StandardError.new
|
46
|
+
ExceptionNotifier.notify_exception(exception)
|
47
|
+
assert notifier1_calls == 1
|
48
|
+
assert notifier2_calls == 1
|
49
|
+
|
50
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :notifier1})
|
51
|
+
assert notifier1_calls == 2
|
52
|
+
assert notifier2_calls == 1
|
53
|
+
|
54
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :notifier2})
|
55
|
+
assert notifier1_calls == 2
|
56
|
+
assert notifier2_calls == 2
|
57
|
+
|
58
|
+
ExceptionNotifier.unregister_exception_notifier(:notifier1)
|
59
|
+
ExceptionNotifier.unregister_exception_notifier(:notifier2)
|
60
|
+
assert ExceptionNotifier.notifiers == [:email]
|
61
|
+
end
|
62
|
+
|
63
|
+
test "should ignore exception if satisfies conditional ignore" do
|
64
|
+
env = "production"
|
65
|
+
ExceptionNotifier.ignore_if do |exception, options|
|
66
|
+
env != "production"
|
67
|
+
end
|
68
|
+
|
69
|
+
notifier_calls = 0
|
70
|
+
test_notifier = lambda { |exception, options| notifier_calls += 1 }
|
71
|
+
ExceptionNotifier.register_exception_notifier(:test, test_notifier)
|
72
|
+
|
73
|
+
exception = StandardError.new
|
74
|
+
|
75
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :test})
|
76
|
+
assert notifier_calls == 1
|
77
|
+
|
78
|
+
env = "development"
|
79
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :test})
|
80
|
+
assert notifier_calls == 1
|
81
|
+
|
82
|
+
ExceptionNotifier.clear_ignore_conditions!
|
83
|
+
ExceptionNotifier.unregister_exception_notifier(:test)
|
84
|
+
end
|
85
|
+
|
86
|
+
test "should not send notification if one of ignored exceptions" do
|
87
|
+
notifier_calls = 0
|
88
|
+
test_notifier = lambda { |exception, options| notifier_calls += 1 }
|
89
|
+
ExceptionNotifier.register_exception_notifier(:test, test_notifier)
|
90
|
+
|
91
|
+
exception = StandardError.new
|
92
|
+
|
93
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :test})
|
94
|
+
assert notifier_calls == 1
|
95
|
+
|
96
|
+
ExceptionNotifier.notify_exception(exception, {:notifiers => :test, :ignore_exceptions => 'StandardError' })
|
97
|
+
assert notifier_calls == 1
|
98
|
+
|
99
|
+
ExceptionNotifier.unregister_exception_notifier(:test)
|
100
|
+
end
|
101
|
+
end
|