exception_notification 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,17 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+
6
+ unless File.exists? "test/dummy/db/test.sqlite3"
7
+ sh "cd test/dummy; rake db:migrate; rake db:test:prepare; cd ../../;"
8
+ end
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ task :default => :test
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+
3
+ class BackgroundExceptionNotificationTest < ActiveSupport::TestCase
4
+ setup do
5
+ begin
6
+ 1/0
7
+ rescue => e
8
+ @exception = e
9
+ @mail = ExceptionNotifier::Notifier.background_exception_notification(@exception)
10
+ end
11
+ end
12
+
13
+ test "should have raised an exception" do
14
+ assert_not_nil @exception
15
+ end
16
+
17
+ test "should have generated a notification email" do
18
+ assert_not_nil @mail
19
+ end
20
+
21
+ test "mail should have a from address set" do
22
+ assert @mail.from == ["dummynotifier@example.com"]
23
+ end
24
+
25
+ test "mail should have a to address set" do
26
+ assert @mail.to == ["dummyexceptions@example.com"]
27
+ end
28
+
29
+ test "mail should have a descriptive subject" do
30
+ assert @mail.subject.include? "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\""
31
+ end
32
+
33
+ test "mail should say exception was raised in background" do
34
+ assert @mail.body.include? "A ZeroDivisionError occurred in background"
35
+ end
36
+
37
+ test "mail should contain backtrace in body" do
38
+ assert @mail.body.include? "test/background_exception_notification_test.rb:6"
39
+ end
40
+
41
+ test "mail should not contain any attachments" do
42
+ assert @mail.attachments == []
43
+ end
44
+
45
+ test "should not send notification if one of ignored exceptions" do
46
+ begin
47
+ raise ActiveRecord::RecordNotFound
48
+ rescue => e
49
+ @ignored_exception = e
50
+ unless ExceptionNotifier.default_ignore_exceptions.include?(@ignored_exception.class)
51
+ @ignored_mail = ExceptionNotifier::Notifier.background_exception_notification(@ignored_exception)
52
+ end
53
+ end
54
+
55
+ assert @ignored_exception.class.inspect == "ActiveRecord::RecordNotFound"
56
+ assert_nil @ignored_mail
57
+ end
58
+ end
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ class PostsControllerTest < ActionController::TestCase
4
+ setup do
5
+ begin
6
+ @post = posts(:one)
7
+ post :create, :post => @post.attributes
8
+ rescue => e
9
+ @exception = e
10
+ @mail = ExceptionNotifier::Notifier.exception_notification(request.env, @exception)
11
+ end
12
+ end
13
+
14
+ test "should have raised an exception" do
15
+ assert_not_nil @exception
16
+ end
17
+
18
+ test "should have generated a notification email" do
19
+ assert_not_nil @mail
20
+ end
21
+
22
+ test "mail should have a from address set" do
23
+ assert @mail.from == ["dummynotifier@example.com"]
24
+ end
25
+
26
+ test "mail should have a to address set" do
27
+ assert @mail.to == ["dummyexceptions@example.com"]
28
+ end
29
+
30
+ test "mail should have a descriptive subject" do
31
+ assert @mail.subject.include? "[Dummy ERROR] # (NoMethodError)"
32
+ end
33
+
34
+ test "mail should contain backtrace in body" do
35
+ assert @mail.body.include? "`method_missing'\n app/controllers/posts_controller.rb:17:in `create'\n"
36
+ end
37
+
38
+ test "should filter sensible data" do
39
+ assert @mail.body.include? "secret\"=>\"[FILTERED]"
40
+ end
41
+
42
+ test "mail should not contain any attachments" do
43
+ assert @mail.attachments == []
44
+ end
45
+
46
+ test "should not send notification if one of ignored exceptions" do
47
+ begin
48
+ get :show, :id => @post.to_param + "10"
49
+ rescue => e
50
+ @ignored_exception = e
51
+ unless ExceptionNotifier.default_ignore_exceptions.include?(@ignored_exception.class)
52
+ @ignored_mail = ExceptionNotifier::Notifier.exception_notification(request.env, @ignored_exception)
53
+ end
54
+ end
55
+
56
+ assert @ignored_exception.class.inspect == "ActiveRecord::RecordNotFound"
57
+ assert_nil @ignored_mail
58
+ end
59
+
60
+ test "should filter session_id on secure requests" do
61
+ request.env['HTTPS'] = 'on'
62
+ begin
63
+ @post = posts(:one)
64
+ post :create, :post => @post.attributes
65
+ rescue => e
66
+ @secured_mail = ExceptionNotifier::Notifier.exception_notification(request.env, e)
67
+ end
68
+
69
+ assert request.ssl?
70
+ assert @secured_mail.body.include? "* session id: [FILTERED]\n *"
71
+ end
72
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class ExceptionNotificationTest < ActiveSupport::TestCase
4
+ test "should have default ignored exceptions" do
5
+ assert ExceptionNotifier.default_ignore_exceptions == [ActiveRecord::RecordNotFound, AbstractController::ActionNotFound, ActionController::RoutingError]
6
+ end
7
+
8
+ test "should have default sender address overriden" do
9
+ assert ExceptionNotifier::Notifier.default_sender_address == %("Dummy Notifier" <dummynotifier@example.com>)
10
+ end
11
+
12
+ test "should have default email prefix overriden" do
13
+ assert ExceptionNotifier::Notifier.default_email_prefix == "[Dummy ERROR] "
14
+ end
15
+
16
+ test "should have default sections" do
17
+ assert ExceptionNotifier::Notifier.default_sections == %w(request session environment backtrace)
18
+ end
19
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exception_notification
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 4
9
- - 1
10
- version: 2.4.1
8
+ - 5
9
+ - 0
10
+ version: 2.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jamis Buck
@@ -16,9 +16,57 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-06-09 00:00:00 Z
20
- dependencies: []
21
-
19
+ date: 2011-08-27 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: actionmailer
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 15
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 4
35
+ version: 3.0.4
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: rails
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 15
47
+ segments:
48
+ - 3
49
+ - 0
50
+ - 4
51
+ version: 3.0.4
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: sqlite3
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 19
63
+ segments:
64
+ - 1
65
+ - 3
66
+ - 4
67
+ version: 1.3.4
68
+ type: :development
69
+ version_requirements: *id003
22
70
  description:
23
71
  email: smartinez87@gmail.com
24
72
  executables: []
@@ -28,16 +76,12 @@ extensions: []
28
76
  extra_rdoc_files: []
29
77
 
30
78
  files:
31
- - README.md
32
- - lib/exception_notification.rb
33
- - lib/exception_notifier/notifier.rb
34
- - lib/exception_notifier/views/exception_notifier/_backtrace.text.erb
35
- - lib/exception_notifier/views/exception_notifier/_environment.text.erb
36
- - lib/exception_notifier/views/exception_notifier/_request.text.erb
37
- - lib/exception_notifier/views/exception_notifier/_session.text.erb
38
- - lib/exception_notifier/views/exception_notifier/_title.text.erb
39
- - lib/exception_notifier/views/exception_notifier/exception_notification.text.erb
40
- - lib/exception_notifier.rb
79
+ - Rakefile
80
+ - .gemtest
81
+ - test/exception_notification_test.rb
82
+ - test/dummy/test/functional/posts_controller_test.rb
83
+ - test/background_exception_notification_test.rb
84
+ has_rdoc: true
41
85
  homepage:
42
86
  licenses: []
43
87
 
@@ -67,9 +111,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
111
  requirements: []
68
112
 
69
113
  rubyforge_project:
70
- rubygems_version: 1.7.1
114
+ rubygems_version: 1.5.0
71
115
  signing_key:
72
116
  specification_version: 3
73
117
  summary: Exception notification by email for Rails apps
74
- test_files: []
75
-
118
+ test_files:
119
+ - test/exception_notification_test.rb
120
+ - test/dummy/test/functional/posts_controller_test.rb
121
+ - test/background_exception_notification_test.rb
data/README.md DELETED
@@ -1,95 +0,0 @@
1
- Exception Notifier Plugin for Rails
2
- ====
3
-
4
- The Exception Notifier plugin provides a mailer object and a default set of
5
- templates for sending email notifications when errors occur in a Rails
6
- application. The plugin is configurable, allowing programmers to specify:
7
-
8
- * the sender address of the email
9
- * the recipient addresses
10
- * the text used to prefix the subject line
11
-
12
- The email includes information about the current request, session, and
13
- environment, and also gives a backtrace of the exception.
14
-
15
- Installation
16
- ---
17
-
18
- You can use the latest ExceptionNotification gem with Rails 3, by adding
19
- the following line in your Gemfile
20
-
21
- gem 'exception_notification', :require => 'exception_notifier'
22
-
23
- As of Rails 3 ExceptionNotification is used as a rack middleware, so you can
24
- configure its options on your config.ru file, or in the environment you
25
- want it to run. In most cases you would want ExceptionNotification to
26
- run on production. You can make it work by
27
-
28
- Whatever::Application.config.middleware.use ExceptionNotifier,
29
- :email_prefix => "[Whatever] ",
30
- :sender_address => %{"notifier" <notifier@example.com>},
31
- :exception_recipients => %w{exceptions@example.com}
32
-
33
- Customization
34
- ---
35
-
36
- By default, the notification email includes four parts: request, session,
37
- environment, and backtrace (in that order). You can customize how each of those
38
- sections are rendered by placing a partial named for that part in your
39
- app/views/exception_notifier directory (e.g., _session.rhtml). Each partial has
40
- access to the following variables:
41
-
42
- @controller # the controller that caused the error
43
- @request # the current request object
44
- @exception # the exception that was raised
45
- @backtrace # a sanitized version of the exception's backtrace
46
- @data # a hash of optional data values that were passed to the notifier
47
- @sections # the array of sections to include in the email
48
-
49
- You can reorder the sections, or exclude sections completely, by altering the
50
- ExceptionNotifier.sections variable. You can even add new sections that
51
- describe application-specific data--just add the section's name to the list
52
- (whereever you'd like), and define the corresponding partial. Then, if your
53
- new section requires information that isn't available by default, make sure
54
- it is made available to the email using the exception_data macro:
55
-
56
- class ApplicationController < ActionController::Base
57
- before_filter :log_additional_data
58
- ...
59
- protected
60
- def log_additional_data
61
- request.env["exception_notifier.exception_data"] = {
62
- :document => @document,
63
- :person => @person
64
- }
65
- end
66
- ...
67
- end
68
-
69
- In the above case, @document and @person would be made available to the email
70
- renderer, allowing your new section(s) to access and display them. See the
71
- existing sections defined by the plugin for examples of how to write your own.
72
-
73
- Notification
74
- ---
75
-
76
- After an exception notification has been delivered the rack environment variable
77
- 'exception_notifier.delivered' will be set to +true+.
78
-
79
- Rails 2.3 stable and earlier
80
- ---
81
-
82
- If you are running Rails 2.3 then see the branch for that:
83
-
84
- <a href="http://github.com/smartinez87/exception_notification/tree/2-3-stable">http://github.com/smartinez87/exception_notification/tree/2-3-stable</a>
85
-
86
- If you are running pre-rack Rails then see this tag:
87
-
88
- <a href="http://github.com/smartinez87/exception_notification/tree/pre-2-3">http://github.com/smartinez87/exception_notification/tree/pre-2-3</a>
89
-
90
- Support and tickets
91
- ---
92
-
93
- <a href="https://github.com/smartinez87/exception_notification/issues">https://github.com/smartinez87/exception_notification/issues</a>
94
-
95
- Copyright (c) 2005 Jamis Buck, released under the MIT license
@@ -1 +0,0 @@
1
- require 'exception_notifier'
@@ -1,37 +0,0 @@
1
- require 'action_dispatch'
2
- require 'exception_notifier/notifier'
3
-
4
- class ExceptionNotifier
5
- def self.default_ignore_exceptions
6
- [].tap do |exceptions|
7
- exceptions << ::ActiveRecord::RecordNotFound if defined? ::ActiveRecord::RecordNotFound
8
- exceptions << ::AbstractController::ActionNotFound if defined? ::AbstractController::ActionNotFound
9
- exceptions << ::ActionController::RoutingError if defined? ::ActionController::RoutingError
10
- end
11
- end
12
-
13
- def initialize(app, options = {})
14
- @app, @options = app, options
15
-
16
- Notifier.default_sender_address = @options[:sender_address]
17
- Notifier.default_exception_recipients = @options[:exception_recipients]
18
- Notifier.default_email_prefix = @options[:email_prefix]
19
- Notifier.default_sections = @options[:sections]
20
-
21
- @options[:ignore_exceptions] ||= self.class.default_ignore_exceptions
22
- end
23
-
24
- def call(env)
25
- @app.call(env)
26
- rescue Exception => exception
27
- options = (env['exception_notifier.options'] ||= Notifier.default_options)
28
- options.reverse_merge!(@options)
29
-
30
- unless Array.wrap(options[:ignore_exceptions]).include?(exception.class)
31
- Notifier.exception_notification(env, exception).deliver
32
- env['exception_notifier.delivered'] = true
33
- end
34
-
35
- raise exception
36
- end
37
- end
@@ -1,89 +0,0 @@
1
- require 'action_mailer'
2
- require 'pp'
3
-
4
- class ExceptionNotifier
5
- class Notifier < ActionMailer::Base
6
- self.mailer_name = 'exception_notifier'
7
- self.append_view_path "#{File.dirname(__FILE__)}/views"
8
-
9
- class << self
10
- attr_writer :default_sender_address
11
- attr_writer :default_exception_recipients
12
- attr_writer :default_email_prefix
13
- attr_writer :default_sections
14
-
15
- def default_sender_address
16
- @default_sender_address || %("Exception Notifier" <exception.notifier@default.com>)
17
- end
18
-
19
- def default_exception_recipients
20
- @default_exception_recipients || []
21
- end
22
-
23
- def default_email_prefix
24
- @default_email_prefix || "[ERROR] "
25
- end
26
-
27
- def default_sections
28
- @default_sections || %w(request session environment backtrace)
29
- end
30
-
31
- def default_options
32
- { :sender_address => default_sender_address,
33
- :exception_recipients => default_exception_recipients,
34
- :email_prefix => default_email_prefix,
35
- :sections => default_sections }
36
- end
37
- end
38
-
39
- class MissingController
40
- def method_missing(*args, &block)
41
- end
42
- end
43
-
44
- def exception_notification(env, exception)
45
- @env = env
46
- @exception = exception
47
- @options = (env['exception_notifier.options'] || {}).reverse_merge(self.class.default_options)
48
- @kontroller = env['action_controller.instance'] || MissingController.new
49
- @request = ActionDispatch::Request.new(env)
50
- @backtrace = clean_backtrace(exception)
51
- @sections = @options[:sections]
52
- data = env['exception_notifier.exception_data'] || {}
53
-
54
- data.each do |name, value|
55
- instance_variable_set("@#{name}", value)
56
- end
57
-
58
- prefix = "#{@options[:email_prefix]}#{@kontroller.controller_name}##{@kontroller.action_name}"
59
- subject = "#{prefix} (#{@exception.class}) #{@exception.message.inspect}"
60
- subject = subject.length > 120 ? subject[0...120] + "..." : subject
61
-
62
- mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format|
63
- format.text { render "#{mailer_name}/exception_notification" }
64
- end
65
- end
66
-
67
- private
68
-
69
- def clean_backtrace(exception)
70
- Rails.respond_to?(:backtrace_cleaner) ?
71
- Rails.backtrace_cleaner.send(:filter, exception.backtrace) :
72
- exception.backtrace
73
- end
74
-
75
- helper_method :inspect_object
76
-
77
- def inspect_object(object)
78
- case object
79
- when Hash, Array
80
- object.inspect
81
- when ActionController::Base
82
- "#{object.controller_name}##{object.action_name}"
83
- else
84
- object.to_s
85
- end
86
- end
87
-
88
- end
89
- end
@@ -1 +0,0 @@
1
- <%= raw @backtrace.join("\n") %>
@@ -1,8 +0,0 @@
1
- <% filtered_env = @request.filtered_env -%>
2
- <% max = filtered_env.keys.max { |a, b| a.length <=> b.length } -%>
3
- <% filtered_env.keys.sort.each do |key| -%>
4
- * <%= raw("%-*s: %s" % [max.length, key, inspect_object(filtered_env[key])]) %>
5
- <% end -%>
6
-
7
- * Process: <%= raw $$ %>
8
- * Server : <%= raw `hostname -s`.chomp %>
@@ -1,4 +0,0 @@
1
- * URL : <%= raw @request.url %>
2
- * IP address: <%= raw @request.remote_ip %>
3
- * Parameters: <%= raw @request.filtered_parameters.inspect %>
4
- * Rails root: <%= raw Rails.root %>
@@ -1,2 +0,0 @@
1
- * session id: <%= raw @request.session['session_id'].inspect.html_safe %>
2
- * data: <%= raw PP.pp(@request.session, "") %>
@@ -1,3 +0,0 @@
1
- -------------------------------
2
- <%= raw title.to_s.humanize %>:
3
- -------------------------------
@@ -1,13 +0,0 @@
1
- A <%= @exception.class %> occurred in <%= @kontroller.controller_name %>#<%= @kontroller.action_name %>:
2
-
3
- <%= raw @exception.message %>
4
- <%= raw @backtrace.first %>
5
-
6
- <% sections = @sections.map do |section|
7
- summary = render(section).strip
8
- unless summary.blank?
9
- title = render("title", :title => section).strip
10
- "#{title}\n\n#{summary.gsub(/^/, " ")}\n\n"
11
- end
12
- end %>
13
- <%= raw sections.join %>