module ExceptionNotifier
Public Class Methods
Source
# File lib/exception_notifier.rb, line 121 def clear_ignore_conditions! @@ignores.clear @@by_notifier_ignores.clear end
Source
# File lib/exception_notifier.rb, line 115 def ignore_crawlers(crawlers) ignore_if do |_exception, opts| opts.key?(:env) && from_crawler(opts[:env], crawlers) end end
Source
# File lib/exception_notifier.rb, line 107 def ignore_if(&block) @@ignores << block end
Adds a condition to decide when an exception must be ignored or not.
ExceptionNotifier.ignore_if do |exception, options| not Rails.env.production? end
Source
# File lib/exception_notifier.rb, line 111 def ignore_notifier_if(notifier, &block) @@by_notifier_ignores[notifier] = block end
Source
# File lib/exception_notifier.rb, line 58 def notify_exception(exception, options = {}, &block) return false if ignored_exception?(options[:ignore_exceptions], exception) return false if ignored?(exception, options) if error_grouping errors_count = group_error!(exception, options) return false unless send_notification?(exception, errors_count) end notification_fired = false selected_notifiers = options.delete(:notifiers) || notifiers [*selected_notifiers].each do |notifier| unless notifier_ignored?(exception, options, notifier: notifier) fire_notification(notifier, exception, options.dup, &block) notification_fired = true end end notification_fired end
Source
# File lib/exception_notifier.rb, line 79 def register_exception_notifier(name, notifier_or_options) if notifier_or_options.respond_to?(:call) @@notifiers[name] = notifier_or_options elsif notifier_or_options.is_a?(Hash) create_and_register_notifier(name, notifier_or_options) else raise ArgumentError, "Invalid notifier '#{name}' defined as #{notifier_or_options.inspect}" end end
Also aliased as: add_notifier
Source
# File lib/exception_notifier.rb, line 94 def registered_exception_notifier(name) @@notifiers[name] end
Source
# File lib/exception_notifier.rb, line 54 def testing_mode! self.testing_mode = true end
Source
# File lib/exception_notifier.rb, line 90 def unregister_exception_notifier(name) @@notifiers.delete(name) end
Private Class Methods
Source
# File lib/exception_notifier.rb, line 173 def create_and_register_notifier(name, options) notifier_classname = "#{name}_notifier".camelize notifier_class = ExceptionNotifier.const_get(notifier_classname) notifier = notifier_class.new(options) register_exception_notifier(name, notifier) rescue NameError => e raise UndefinedNotifierError, "No notifier named '#{name}' was found. Please, revise your configuration options. Cause: #{e.message}" end
Source
# File lib/exception_notifier.rb, line 160 def fire_notification(notifier_name, exception, options, &block) notifier = registered_exception_notifier(notifier_name) notifier.call(exception, options, &block) rescue Exception => e # standard:disable Lint/RescueException raise e if @@testing_mode logger.warn( "An error occurred when sending a notification using '#{notifier_name}' notifier." \ "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}" ) false end
Source
# File lib/exception_notifier.rb, line 183 def from_crawler(env, ignored_crawlers) agent = env["HTTP_USER_AGENT"] Array(ignored_crawlers).any? do |crawler| agent =~ Regexp.new(crawler) end end
Source
# File lib/exception_notifier.rb, line 128 def ignored?(exception, options) @@ignores.any? { |condition| condition.call(exception, options) } rescue Exception => e # standard:disable Lint/RescueException raise e if @@testing_mode logger.warn( "An error occurred when evaluating an ignore condition. #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}" ) false end
Source
# File lib/exception_notifier.rb, line 154 def ignored_exception?(ignore_array, exception) all_ignored_exceptions = (Array(ignored_exceptions) + Array(ignore_array)).map(&:to_s) exception_ancestors = exception.singleton_class.ancestors.map(&:to_s) !(all_ignored_exceptions & exception_ancestors).empty? end
Source
# File lib/exception_notifier.rb, line 139 def notifier_ignored?(exception, options, notifier:) return false unless @@by_notifier_ignores.key?(notifier) condition = @@by_notifier_ignores[notifier] condition.call(exception, options) rescue Exception => e # standard:disable Lint/RescueException raise e if @@testing_mode logger.warn(<<~"MESSAGE") An error occurred when evaluating a by-notifier ignore condition. #{e.class}: #{e.message} #{e.backtrace.join("\n")} MESSAGE false end