Skip to content

Commit

Permalink
Add :ignore_crawlers options.
Browse files Browse the repository at this point in the history
  • Loading branch information
smartinez87 committed Oct 23, 2011
1 parent 72defaa commit 2e36818
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ run on production. You can make it work by

Customization
---

### Sections

By default, the notification email includes four parts: request, session,
environment, and backtrace (in that order). You can customize how each of those
sections are rendered by placing a partial named for that part in your
Expand All @@ -48,7 +51,7 @@ access to the following variables:
You can reorder the sections, or exclude sections completely, by altering the
ExceptionNotifier.sections variable. You can even add new sections that
describe application-specific data--just add the section's name to the list
(wherever you'd like), and define the corresponding partial.
(wherever you'd like), and define the corresponding partial.

#Example with two new added sections
Whatever::Application.config.middleware.use ExceptionNotifier,
Expand Down Expand Up @@ -76,14 +79,13 @@ it is made available to the email using the exception_data macro:
In the above case, @document and @person would be made available to the email
renderer, allowing your new section(s) to access and display them. See the
existing sections defined by the plugin for examples of how to write your own.
You can also choose to exclude the exception message from the subject, which is included by default.
Use _:verbose_subject => false_ to exclude it.

### Ignore Exceptions

You can also ignore types of exceptions, which will make
ExceptionNotifier avoid sending notifications for the specified exception types.
To achieve that, you should use the _:ignore_exceptions_ option, like this:

#Example ignoring some exceptions
Whatever::Application.config.middleware.use ExceptionNotifier,
:email_prefix => "[Whatever] ",
:sender_address => %{"notifier" <[email protected]>},
Expand All @@ -96,6 +98,24 @@ By default, ExceptionNotifier ignores _ActiveRecord::RecordNotFound_,
_AbstractController::ActionNotFound_ and
_ActionController::RountingError_.

### Verbose

You can also choose to exclude the exception message from the subject, which is included by default.
Use _:verbose_subject => false_ to exclude it.

### Ignore Crawlers

In some cases you may want to avoid getting notifications from exceptions
made by crawlers. Using _:ignore_crawlers_ options like this,

Whatever::Application.config.middleware.use ExceptionNotifier,
:email_prefix => "[Whatever] ",
:sender_address => %{"notifier" <[email protected]>},
:exception_recipients => %w{[email protected]},
:ignore_crawlers => %w{Googlebot bingbot}

will prevent sending those unwanted notifications.

Background Notifications
---

Expand Down
20 changes: 19 additions & 1 deletion lib/exception_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def self.default_ignore_exceptions
end
end

def self.default_ignore_crawlers
[]
end

def initialize(app, options = {})
@app, @options = app, options

Expand All @@ -20,6 +24,7 @@ def initialize(app, options = {})
Notifier.default_verbose_subject = @options[:verbose_subject]

@options[:ignore_exceptions] ||= self.class.default_ignore_exceptions
@options[:ignore_crawlers] ||= self.class.default_ignore_crawlers
end

def call(env)
Expand All @@ -28,11 +33,24 @@ def call(env)
options = (env['exception_notifier.options'] ||= Notifier.default_options)
options.reverse_merge!(@options)

unless Array.wrap(options[:ignore_exceptions]).include?(exception.class)
unless ignored_exception(options[:ignore_exceptions], exception) || from_crawler(options[:ignore_crawlers], env['HTTP_USER_AGENT'])
Notifier.exception_notification(env, exception).deliver
env['exception_notifier.delivered'] = true
end

raise exception
end

private

def ignored_exception(ignore_array, exception)
Array.wrap(ignore_array).include?(exception.class)
end

def from_crawler(ignore_array, agent)
ignore_array.each do |crawler|
return true if (agent =~ Regexp.new(crawler))
end unless ignore_array.blank?
false
end
end
2 changes: 1 addition & 1 deletion lib/exception_notifier/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def default_email_prefix
def default_sections
@default_sections || %w(request session environment backtrace)
end

def default_verbose_subject
@default_verbose_subject.nil? || @default_verbose_subject
end
Expand Down
22 changes: 13 additions & 9 deletions test/dummy/test/functional/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,24 @@ class PostsControllerTest < ActionController::TestCase
assert @secured_mail.body.include? "* session id: [FILTERED]\n *"
end

test "should not include exception message in subject" do
test "should ignore exception if from unwanted cralwer" do
request.env['HTTP_USER_AGENT'] = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
begin
@post = posts(:one)
post :create, :post => @post.attributes
rescue => e
@exception = e
custom_env = request.env
custom_env['exception_notifier.options']||={}
custom_env['exception_notifier.options'].merge!(:verbose_subject => false)
@mail = ExceptionNotifier::Notifier.exception_notification(custom_env, @exception)
custom_env['exception_notifier.options'] ||= {}
custom_env['exception_notifier.options'].merge!(:ignore_crawlers => %w(Googlebot))
ignore_array = custom_env['exception_notifier.options'][:ignore_crawlers]
unless ExceptionNotifier.new(Dummy::Application, custom_env['exception_notifier.options']).send(:from_crawler, ignore_array, custom_env['HTTP_USER_AGENT'])
@ignored_mail = ExceptionNotifier::Notifier.exception_notification(custom_env, @exception)
end
end
assert_equal "[Dummy ERROR] # (NoMethodError)", @mail.subject

assert_nil @ignored_mail
end

end

class PostsControllerTestWithoutVerboseSubject < ActionController::TestCase
Expand All @@ -98,8 +102,8 @@ class PostsControllerTestWithoutVerboseSubject < ActionController::TestCase
@mail = ExceptionNotifier::Notifier.exception_notification(request.env, @exception)
end
end

test "should not include exception message in subject" do
assert_equal "[Dummy ERROR] # (NoMethodError)", @mail.subject
assert_equal "[ERROR] # (NoMethodError)", @mail.subject
end
end
end
4 changes: 4 additions & 0 deletions test/exception_notification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class ExceptionNotificationTest < ActiveSupport::TestCase
test "should have verbose subject by default" do
assert ExceptionNotifier::Notifier.default_options[:verbose_subject] == true
end

test "should have ignored crawler by default" do
assert ExceptionNotifier.default_ignore_crawlers == []
end
end

0 comments on commit 2e36818

Please sign in to comment.