diff options
Diffstat (limited to 'vendor/plugins/exception_notification/lib')
5 files changed, 251 insertions, 0 deletions
diff --git a/vendor/plugins/exception_notification/lib/exception_notification.rb b/vendor/plugins/exception_notification/lib/exception_notification.rb new file mode 100644 index 000000000..bf5975201 --- /dev/null +++ b/vendor/plugins/exception_notification/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
\ No newline at end of file diff --git a/vendor/plugins/exception_notification/lib/exception_notification/consider_local.rb b/vendor/plugins/exception_notification/lib/exception_notification/consider_local.rb new file mode 100644 index 000000000..6b9e236f7 --- /dev/null +++ b/vendor/plugins/exception_notification/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 diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifiable.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifiable.rb new file mode 100644 index 000000000..19895e8db --- /dev/null +++ b/vendor/plugins/exception_notification/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.to_s) + 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 diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb new file mode 100644 index 000000000..2cab3f963 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/notifier.rb @@ -0,0 +1,80 @@ +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 ExceptionNotification::Notifier < ActionMailer::Base + self.mailer_name = 'exception_notifier' + self.view_paths << "#{File.dirname(__FILE__)}/../../views" + + # next line is a hack to fix + # undefined method `find_template' for #<Array:0x000001009cd230> + # after Rails 2.3.8 -> 2.3.11 upgrade + self.view_paths = ActionView::PathSet.new(self.view_paths) unless self.view_paths.respond_to?(:find_template) + + @@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={}) + source = self.class.exception_source(controller) + content_type "text/plain" + + subject "#{email_prefix}#{source} (#{exception.class}) #{exception.message.inspect}" + + recipients exception_recipients + from sender_address + + body data.merge({ :controller => controller, :request => request, + :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 + + 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 diff --git a/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb b/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb new file mode 100644 index 000000000..942e1c527 --- /dev/null +++ b/vendor/plugins/exception_notification/lib/exception_notification/notifier_helper.rb @@ -0,0 +1,67 @@ +require 'pp' + +# 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::NotifierHelper + PARAM_FILTER_REPLACEMENT = "[FILTERED]" + + def render_section(section) + RAILS_DEFAULT_LOGGER.info("rendering section #{section.inspect}") + summary = render("exception_notifier/#{section}").strip + unless summary.blank? + title = render("exception_notifier/title", :locals => { :title => section }).strip + "#{title}\n\n#{summary.gsub(/^/, " ")}\n\n" + end + end + + def inspect_model_object(model, locals={}) + render('exception_notifier/inspect_model', + :locals => { :inspect_model => model, + :show_instance_variables => true, + :show_attributes => true }.merge(locals)) + end + + def inspect_value(value) + len = 512 + result = object_to_yaml(value).gsub(/\n/, "\n ").strip + result = result[0,len] + "... (#{result.length-len} bytes more)" if result.length > len+20 + result + end + + def object_to_yaml(object) + object.to_yaml.sub(/^---\s*/m, "") + end + + def exclude_raw_post_parameters? + @controller && @controller.respond_to?(:filter_parameters) + end + + def filter_sensitive_post_data_parameters(parameters) + exclude_raw_post_parameters? ? @controller.__send__(:filter_parameters, parameters) : parameters + end + + def filter_sensitive_post_data_from_env(env_key, env_value) + return env_value unless exclude_raw_post_parameters? + return PARAM_FILTER_REPLACEMENT if (env_key =~ /RAW_POST_DATA/i) + return @controller.__send__(:filter_parameters, {env_key => env_value}).values[0] + end + +end |