Permalink
Browse files

blah

  • Loading branch information...
1 parent 1684f26 commit 7d81bf1995c5b92ee79045ccfdf35b7d144b119d @justinko committed Mar 10, 2010
@@ -2,6 +2,7 @@
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
+ include ExceptionNotifiable
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
@@ -0,0 +1,2 @@
+ExceptionNotifier.exception_recipients = %w(jko170@gmail.com)
+ExceptionNotifier.sender_address = %("Quizdoo Error" <error@quizdoo.com>)
@@ -0,0 +1,140 @@
+= Exception Notifier Plugin for Rails
+
+The Exception Notifier plugin provides a mailer object and a default set of
+templates for sending email notifications when errors occur in a Rails
+application. The plugin is configurable, allowing programmers to specify:
+
+* the sender address of the email
+* the recipient addresses
+* the text used to prefix the subject line
+
+The email includes information about the current request, session, and
+environment, and also gives a backtrace of the exception.
+
+== Usage
+
+First, include the ExceptionNotifiable mixin in whichever controller you want
+to generate error emails (typically ApplicationController):
+
+ class ApplicationController < ActionController::Base
+ include ExceptionNotifiable
+ ...
+ end
+
+Then, specify the email recipients in your environment:
+
+ ExceptionNotifier.exception_recipients = %w(joe@schmoe.com bill@schmoe.com)
+
+And that's it! The defaults take care of the rest.
+
+== Configuration
+
+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 =
+ %("Application Error" <app.error@myapp.com>)
+
+ # defaults to "[ERROR] "
+ ExceptionNotifier.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
+get a detailed error instead of the generic error page. You do this in your
+controller (or even per-controller):
+
+ consider_local "64.72.18.143", "14.17.21.25"
+
+You can specify subnet masks as well, so that all matching addresses are
+considered local:
+
+ consider_local "64.72.18.143/24"
+
+The address "127.0.0.1" is always considered local. If you want to completely
+reset the list of all addresses (for instance, if you wanted "127.0.0.1" to
+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 ConsiderLocal
+ end
+
+== Customization
+
+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
+app/views/exception_notifier directory (e.g., _session.rhtml). Each partial has
+access to the following variables:
+
+* @controller: the controller that caused the error
+* @request: the current request object
+* @exception: the exception that was raised
+* @host: the name of the host that made the request
+* @backtrace: a sanitized version of the exception's backtrace
+* @rails_root: a sanitized version of RAILS_ROOT
+* @data: a hash of optional data values that were passed to the notifier
+* @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
+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
+it is made available to the email using the exception_data macro:
+
+ class ApplicationController < ActionController::Base
+ ...
+ protected
+ exception_data :additional_data
+
+ def additional_data
+ { :document => @document,
+ :person => @person }
+ end
+ ...
+ end
+
+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.
+
+== 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
+
+Copyright (c) 2005 Jamis Buck, released under the MIT license
@@ -0,0 +1,5 @@
+require "action_mailer"
+require "exception_notifier"
+require "exception_notifiable"
+require "exception_notifier_helper"
+require "consider_local"
@@ -0,0 +1,31 @@
+#This didn't belong on ExceptionNotifier and made backtraces worse. To keep original functionality in place
+#'ActionController::Base.send :include, ConsiderLocal' or just include in your controller
+module ConsiderLocal
+ module ClassMethods
+ def self.included(target)
+ require 'ipaddr'
+ target.extend(ClassMethods)
+ end
+
+ 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
@@ -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 ExceptionNotifiable
+ 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
+
+ ExceptionNotifier.deliver_exception_notification(exception, self, request, data)
+ end
+end
@@ -0,0 +1,75 @@
+require 'pathname'
+
+# 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.
+class ExceptionNotifier < 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
+
+ @@exception_recipients = []
+ cattr_accessor :exception_recipients
+
+ @@email_prefix = "[ERROR] "
+ cattr_accessor :email_prefix
+
+ @@sections = %w(request session environment backtrace)
+ cattr_accessor :sections
+
+ def self.reloadable?() false end
+
+ def exception_notification(exception, controller, request, data={})
+ content_type "text/plain"
+
+ subject "#{email_prefix}#{ExceptionNotifier.exception_source(controller)} (#{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"]),
+ :backtrace => sanitize_backtrace(exception.backtrace),
+ :rails_root => rails_root, :data => data,
+ :sections => sections })
+ end
+
+ 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
+
+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
Oops, something went wrong.

0 comments on commit 7d81bf1

Please sign in to comment.