Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

2 3 stable #6

Open
wants to merge 18 commits into from

4 participants

@azimux

local_addresses doesn't exist after a "include ExceptionNotification::ConsiderLocal"

This is because self.include is defined in ExceptionNotification::ConsiderLocal::ClassMethods instead of ExceptionNotification::ConsiderLocal

@azimux

Hmmm... I don't know how to make it request a pull only from my 2-3-stable into rails:2-3-stable

Obviously only 1 commit should be pulled into 2-3-stable and not 17 into master

@albeit

I just installed this plugin and didn't realize I had to add:

config.gem "exception_notification"

to Rails::Initializer.run. Could this be added to the README, just so others avoid the issue?

Thanks.

@jjulian

This tripped me up for a few minutes too, good fix in 66dc72b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 12, 2010
  1. @timocratic
  2. @timocratic

    no need to override ActionControllers error rendering, since it alrea…

    timocratic authored
    …dy does the 505/404 and any other that exists in public
  3. @timocratic

    add per controller skipping

    timocratic authored
  4. @timocratic
Commits on Feb 13, 2010
  1. @timocratic
  2. @timocratic

    redundant 'in'

    timocratic authored
Commits on Feb 14, 2010
  1. @timocratic
  2. @timocratic

    Added .html.erb extension to partial template search heuristics

    David Black authored timocratic committed
  3. @timocratic

    Fixed wrong variable name (too many 'p*' variables...)

    David Black authored timocratic committed
  4. @timocratic
Commits on Mar 3, 2010
  1. @timocratic

    more callability from metal/rack - missed the subject line reference …

    timocratic authored
    …to action_name
    
    still requires request to be defined
Commits on Mar 4, 2010
  1. @timocratic
  2. @timocratic

    drop render_overridable in favor of letting AV handle it to better ma…

    timocratic authored
    …tch master.
    
    this does create a new ActionView dependency, though
Commits on Mar 14, 2010
  1. @timocratic
Commits on Apr 2, 2010
  1. @timocratic
  2. @timocratic
Commits on Sep 23, 2010
  1. @azimux
Commits on Oct 6, 2010
  1. @azimux

    Test for consider_local and local_addresses controller class methods

    azimux authored
    added by ExceptionNotification::ConsiderLocal
This page is out of date. Refresh to see the latest.
View
59 README
@@ -17,13 +17,13 @@ First, include the ExceptionNotifiable mixin in whichever controller you want
to generate error emails (typically ApplicationController):
class ApplicationController < ActionController::Base
- include ExceptionNotifiable
+ include ExceptionNotification::Notifiable
...
end
Then, specify the email recipients in your environment:
- ExceptionNotifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)
+ ExceptionNotification::Notifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)
And that's it! The defaults take care of the rest.
@@ -33,11 +33,20 @@ You can tweak other values to your liking, as well. In your environment file,
just set any or all of the following values:
# defaults to exception.notifier@default.com
- ExceptionNotifier.sender_address =
+ ExceptionNotification::Notifier.sender_address =
%("Application Error" <app.error@myapp.com>)
# defaults to "[ERROR] "
- ExceptionNotifier.email_prefix = "[APP] "
+ ExceptionNotification::Notifier.email_prefix = "[APP] "
+
+Even if you have mixed into ApplicationController you can skip notification in
+some controllers by
+
+ class MyController < ApplicationController
+ skip_exception_notifications
+ end
+
+== Deprecated local_request? overriding
Email notifications will only occur when the IP address is determined not to
be local. You can specify certain addresses to always be local so that you'll
@@ -57,6 +66,19 @@ NOT be considered local), you can simply do, somewhere in your controller:
local_addresses.clear
+NOTE: The above functionality has has been pulled out to consider_local.rb,
+as interfering with rails local determination is orthogonal to notification,
+unnecessarily clutters backtraces, and even occasionally errs on odd ip or
+requests bugs. To return original functionality add an initializer with:
+
+ ActionController::Base.send :include, ConsiderLocal
+
+or just include it per controller that wants it
+
+ class MyController < ApplicationController
+ include ExceptionNotification::ConsiderLocal
+ end
+
== Customization
By default, the notification email includes four parts: request, session,
@@ -75,7 +97,7 @@ access to the following variables:
* @sections: the array of sections to include in the email
You can reorder the sections, or exclude sections completely, by altering the
-ExceptionNotifier.sections variable. You can even add new sections that
+ExceptionNotification::Notifier.sections variable. You can even add new sections that
describe application-specific data--just add the section's name to the list
(whereever you'd like), and define the corresponding partial. Then, if your
new section requires information that isn't available by default, make sure
@@ -97,15 +119,26 @@ 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.
-== Advanced Customization
+== 404s errors
+
+Notification is skipped if you return a 404 status, which happens by default
+for an ActiveRecord::RecordNotFound or ActionController::UnknownAction error.
+
+== Manually notifying of error in a rescue block
+
+If your controller action manually handles an error, the notifier will never be
+run. To manually notify of an error call notify_about_exception from within the
+rescue block
+
+ def index
+ #risky operation here
+ rescue StandardError => error
+ #custom error handling here
+ notify_about_exception(error)
+ end
-By default, the email notifier will only notify on critical errors. For
-ActiveRecord::RecordNotFound and ActionController::UnknownAction, it will
-simply render the contents of your public/404.html file. Other exceptions
-will render public/500.html and will send the email notification. If you want
-to use different rules for the notification, you will need to implement your
-own rescue_action_in_public method. You can look at the default implementation
-in ExceptionNotifiable for an example of how to go about that.
+== Support and tickets
+https://rails.lighthouseapp.com/projects/8995-rails-plugins
Copyright (c) 2005 Jamis Buck, released under the MIT license
View
11 exception_notification.gemspec
@@ -0,0 +1,11 @@
+Gem::Specification.new do |s|
+ s.name = 'exception_notification'
+ s.version = '2.3.3.0'
+ s.authors = ["Jamis Buck", "Josh Peek", "Tim Connor"]
+ s.date = %q{2010-03-13}
+ s.summary = "Exception notification by email for Rails apps - 2.3-stable compatible version"
+ s.email = "timocratic@gmail.com"
+
+ s.files = ['README'] + Dir['lib/**/*'] + Dir['views/**/*']
+ s.require_path = 'lib'
+end
View
5 init.rb
@@ -1,4 +1 @@
-require "action_mailer"
-require "exception_notifier"
-require "exception_notifiable"
-require "exception_notifier_helper"
+require "exception_notification"
View
99 lib/exception_notifiable.rb
@@ -1,99 +0,0 @@
-require 'ipaddr'
-
-# Copyright (c) 2005 Jamis Buck
-#
-# Permission is hereby granted, free of charge, to any person obtaining
-# a copy of this software and associated documentation files (the
-# "Software"), to deal in the Software without restriction, including
-# without limitation the rights to use, copy, modify, merge, publish,
-# distribute, sublicense, and/or sell copies of the Software, and to
-# permit persons to whom the Software is furnished to do so, subject to
-# the following conditions:
-#
-# The above copyright notice and this permission notice shall be
-# included in all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-module ExceptionNotifiable
- def self.included(target)
- target.extend(ClassMethods)
- end
-
- module ClassMethods
- def consider_local(*args)
- local_addresses.concat(args.flatten.map { |a| IPAddr.new(a) })
- end
-
- def local_addresses
- addresses = read_inheritable_attribute(:local_addresses)
- unless addresses
- addresses = [IPAddr.new("127.0.0.1")]
- write_inheritable_attribute(:local_addresses, addresses)
- end
- addresses
- end
-
- def exception_data(deliverer=self)
- if deliverer == self
- read_inheritable_attribute(:exception_data)
- else
- write_inheritable_attribute(:exception_data, deliverer)
- end
- end
-
- def exceptions_to_treat_as_404
- exceptions = [ActiveRecord::RecordNotFound,
- ActionController::UnknownController,
- ActionController::UnknownAction]
- exceptions << ActionController::RoutingError if ActionController.const_defined?(:RoutingError)
- exceptions
- end
- end
-
- private
-
- def local_request?
- remote = IPAddr.new(request.remote_ip)
- !self.class.local_addresses.detect { |addr| addr.include?(remote) }.nil?
- end
-
- def render_404
- respond_to do |type|
- type.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => "404 Not Found" }
- type.all { render :nothing => true, :status => "404 Not Found" }
- end
- end
-
- def render_500
- respond_to do |type|
- type.html { render :file => "#{RAILS_ROOT}/public/500.html", :status => "500 Error" }
- type.all { render :nothing => true, :status => "500 Error" }
- end
- end
-
- def rescue_action_in_public(exception)
- case exception
- when *self.class.exceptions_to_treat_as_404
- render_404
-
- else
- render_500
-
- deliverer = self.class.exception_data
- data = case deliverer
- when nil then {}
- when Symbol then send(deliverer)
- when Proc then deliverer.call(self)
- end
-
- ExceptionNotifier.deliver_exception_notification(exception, self,
- request, data)
- end
- end
-end
View
7 lib/exception_notification.rb
@@ -0,0 +1,7 @@
+require "action_mailer"
+module ExceptionNotification
+ autoload :Notifiable, 'exception_notification/notifiable'
+ autoload :Notifier, 'exception_notification/notifier'
+ #autoload :NotifierHelper, 'exception_notification/notifier_helper'
+ autoload :ConsiderLocal, 'exception_notification/consider_local'
+end
View
31 lib/exception_notification/consider_local.rb
@@ -0,0 +1,31 @@
+#This didn't belong on ExceptionNotifier and made backtraces worse. To keep original functionality in place
+#'ActionController::Base.send :include, ExceptionNotification::ConsiderLocal' or just include in your controller
+module ExceptionNotification::ConsiderLocal
+ def self.included(target)
+ require 'ipaddr'
+ target.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def consider_local(*args)
+ local_addresses.concat(args.flatten.map { |a| IPAddr.new(a) })
+ end
+
+ def local_addresses
+ addresses = read_inheritable_attribute(:local_addresses)
+ unless addresses
+ addresses = [IPAddr.new("127.0.0.1")]
+ write_inheritable_attribute(:local_addresses, addresses)
+ end
+ addresses
+ end
+ end
+
+private
+
+ def local_request?
+ remote = IPAddr.new(request.remote_ip)
+ !self.class.local_addresses.detect { |addr| addr.include?(remote) }.nil?
+ end
+
+end
View
66 lib/exception_notification/notifiable.rb
@@ -0,0 +1,66 @@
+# Copyright (c) 2005 Jamis Buck
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+module ExceptionNotification::Notifiable
+ def self.included(target)
+ target.extend(ClassMethods)
+ target.skip_exception_notifications false
+ end
+
+ module ClassMethods
+ def exception_data(deliverer=self)
+ if deliverer == self
+ read_inheritable_attribute(:exception_data)
+ else
+ write_inheritable_attribute(:exception_data, deliverer)
+ end
+ end
+
+ def skip_exception_notifications(boolean=true)
+ write_inheritable_attribute(:skip_exception_notifications, boolean)
+ end
+
+ def skip_exception_notifications?
+ read_inheritable_attribute(:skip_exception_notifications)
+ end
+ end
+
+private
+
+ def rescue_action_in_public(exception)
+ super
+ notify_about_exception(exception) if deliver_exception_notification?
+ end
+
+ def deliver_exception_notification?
+ !self.class.skip_exception_notifications? && ![404, "404 Not Found"].include?(response.status)
+ end
+
+ def notify_about_exception(exception)
+ deliverer = self.class.exception_data
+ data = case deliverer
+ when nil then {}
+ when Symbol then send(deliverer)
+ when Proc then deliverer.call(self)
+ end
+
+ ExceptionNotification::Notifier.deliver_exception_notification(exception, self, request, data)
+ end
+end
View
35 lib/exception_notifier.rb → lib/exception_notification/notifier.rb
@@ -20,7 +20,10 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-class ExceptionNotifier < ActionMailer::Base
+class ExceptionNotification::Notifier < ActionMailer::Base
+ self.mailer_name = 'exception_notifier'
+ self.view_paths << "#{File.dirname(__FILE__)}/../../views"
+
@@sender_address = %("Exception Notifier" <exception.notifier@default.com>)
cattr_accessor :sender_address
@@ -33,34 +36,40 @@ class ExceptionNotifier < ActionMailer::Base
@@sections = %w(request session environment backtrace)
cattr_accessor :sections
- self.template_root = "#{File.dirname(__FILE__)}/../views"
-
def self.reloadable?() false end
def exception_notification(exception, controller, request, data={})
+ source = self.class.exception_source(controller)
content_type "text/plain"
- subject "#{email_prefix}#{controller.controller_name}##{controller.action_name} (#{exception.class}) #{exception.message.inspect}"
+ subject "#{email_prefix}#{source} (#{exception.class}) #{exception.message.inspect}"
recipients exception_recipients
from sender_address
body data.merge({ :controller => controller, :request => request,
- :exception => exception, :host => (request.env["HTTP_X_FORWARDED_HOST"] || request.env["HTTP_HOST"]),
+ :exception => exception, :exception_source => source, :host => (request.env["HTTP_X_FORWARDED_HOST"] || request.env["HTTP_HOST"]),
:backtrace => sanitize_backtrace(exception.backtrace),
:rails_root => rails_root, :data => data,
:sections => sections })
end
- private
-
- def sanitize_backtrace(trace)
- re = Regexp.new(/^#{Regexp.escape(rails_root)}/)
- trace.map { |line| Pathname.new(line.gsub(re, "[RAILS_ROOT]")).cleanpath.to_s }
+ def self.exception_source(controller)
+ if controller.respond_to?(:controller_name)
+ "in #{controller.controller_name}##{controller.action_name}"
+ else
+ "outside of a controller"
end
+ end
- def rails_root
- @rails_root ||= Pathname.new(RAILS_ROOT).cleanpath.to_s
- end
+private
+ def sanitize_backtrace(trace)
+ re = Regexp.new(/^#{Regexp.escape(rails_root)}/)
+ trace.map { |line| Pathname.new(line.gsub(re, "[RAILS_ROOT]")).cleanpath.to_s }
+ end
+
+ def rails_root
+ @rails_root ||= Pathname.new(RAILS_ROOT).cleanpath.to_s
+ end
end
View
21 lib/exception_notifier_helper.rb → lib/exception_notification/notifier_helper.rb
@@ -20,32 +20,20 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-module ExceptionNotifierHelper
- VIEW_PATH = "views/exception_notifier"
- APP_PATH = "#{RAILS_ROOT}/app/#{VIEW_PATH}"
+module ExceptionNotification::NotifierHelper
PARAM_FILTER_REPLACEMENT = "[FILTERED]"
def render_section(section)
RAILS_DEFAULT_LOGGER.info("rendering section #{section.inspect}")
- summary = render_overridable(section).strip
+ summary = render("exception_notifier/#{section}").strip
unless summary.blank?
- title = render_overridable(:title, :locals => { :title => section }).strip
+ title = render("exception_notifier/title", :locals => { :title => section }).strip
"#{title}\n\n#{summary.gsub(/^/, " ")}\n\n"
end
end
- def render_overridable(partial, options={})
- if File.exist?(path = "#{APP_PATH}/_#{partial}.rhtml")
- render(options.merge(:file => path, :use_full_path => false))
- elsif File.exist?(path = "#{File.dirname(__FILE__)}/../#{VIEW_PATH}/_#{partial}.rhtml")
- render(options.merge(:file => path, :use_full_path => false))
- else
- ""
- end
- end
-
def inspect_model_object(model, locals={})
- render_overridable(:inspect_model,
+ render('exception_notifier/inspect_model',
:locals => { :inspect_model => model,
:show_instance_variables => true,
:show_attributes => true }.merge(locals))
@@ -75,4 +63,5 @@ def filter_sensitive_post_data_from_env(env_key, env_value)
return PARAM_FILTER_REPLACEMENT if (env_key =~ /RAW_POST_DATA/i)
return @controller.__send__(:filter_parameters, {env_key => env_value}).values[0]
end
+
end
View
24 test/exception_notifier_helper_test.rb
@@ -1,10 +1,10 @@
require 'test_helper'
-require 'exception_notifier_helper'
+require 'exception_notification/notifier_helper'
class ExceptionNotifierHelperTest < Test::Unit::TestCase
class ExceptionNotifierHelperIncludeTarget
- include ExceptionNotifierHelper
+ include ExceptionNotification::NotifierHelper
end
def setup
@@ -37,13 +37,14 @@ def test_should_return_params_if_controller_can_not_filter_parameters
# Controller with filtering
class ControllerWithFilterParameters
- def filter_parameters(params); :filtered end
+ def filter_parameters(params)
+ { "PARAM" => ExceptionNotification::NotifierHelper::PARAM_FILTER_REPLACEMENT }
+ end
end
def test_should_filter_env_values_for_raw_post_data_keys_if_controller_can_filter_parameters
stub_controller(ControllerWithFilterParameters.new)
assert !@helper.filter_sensitive_post_data_from_env("RAW_POST_DATA", "secret").include?("secret")
- assert @helper.filter_sensitive_post_data_from_env("SOME_OTHER_KEY", "secret").include?("secret")
end
def test_should_exclude_raw_post_parameters_if_controller_can_filter_parameters
stub_controller(ControllerWithFilterParameters.new)
@@ -51,9 +52,22 @@ def test_should_exclude_raw_post_parameters_if_controller_can_filter_parameters
end
def test_should_delegate_param_filtering_to_controller_if_controller_can_filter_parameters
stub_controller(ControllerWithFilterParameters.new)
- assert_equal :filtered, @helper.filter_sensitive_post_data_parameters(:params)
+ assert_equal({"PARAM" => "[FILTERED]" }, @helper.filter_sensitive_post_data_parameters({"PARAM" => 'secret'}))
+ end
+
+ # Controller with ConsiderLocal
+
+ class ControllerWithConsiderLocal
+ include ExceptionNotification::ConsiderLocal
end
+ def test_consider_local_and_local_addresses
+ assert_equal [IPAddr.new("127.0.0.1")], ControllerWithConsiderLocal.local_addresses
+ ControllerWithConsiderLocal.consider_local '192.168.1.1'
+ assert_equal [IPAddr.new("127.0.0.1"), IPAddr.new("192.168.1.1")],
+ ControllerWithConsiderLocal.local_addresses
+ end
+
private
def stub_controller(controller)
@helper.instance_variable_set(:@controller, controller)
View
5 test/test_helper.rb
@@ -2,6 +2,7 @@
require 'rubygems'
require 'active_support'
-$:.unshift File.join(File.dirname(__FILE__), '../lib')
-
RAILS_ROOT = '.' unless defined?(RAILS_ROOT)
+
+$:.unshift File.join(File.dirname(__FILE__), '../lib')
+require 'exception_notification'
View
4 views/exception_notifier/_session.rhtml
@@ -1,2 +1,2 @@
-* session id: <%= @request.session.instance_variable_get(:@session_id).inspect %>
-* data: <%= PP.pp(@request.session.instance_variable_get(:@data),"").gsub(/\n/, "\n ").strip %>
+* session id: <%= @request.session_options[:id] %>
+* data: <%= @request.session.inspect %>
View
2  views/exception_notifier/exception_notification.rhtml
@@ -1,4 +1,4 @@
-A <%= @exception.class %> occurred in <%= @controller.controller_name %>#<%= @controller.action_name %>:
+A <%= @exception.class %> occurred <%= @exception_source %>:
<%= @exception.message %>
<%= @backtrace.first %>
Something went wrong with that request. Please try again.