module ExceptionNotifier

Public Class Methods

add_notifier(name, notifier_or_options)
clear_ignore_conditions!() click to toggle source
# File lib/exception_notifier.rb, line 85
def clear_ignore_conditions!
  @@ignores.clear
end
ignore_if(&block) click to toggle source

Adds a condition to decide when an exception must be ignored or not.

ExceptionNotifier.ignore_if do |exception, options|
  not Rails.env.production?
end
# File lib/exception_notifier.rb, line 81
def ignore_if(&block)
  @@ignores << block
end
notifiers() click to toggle source
# File lib/exception_notifier.rb, line 72
def notifiers
  @@notifiers.keys
end
notify_exception(exception, options={}) click to toggle source
# File lib/exception_notifier.rb, line 43
def notify_exception(exception, options={})
  return false if ignored_exception?(options[:ignore_exceptions], exception)
  return false if ignored?(exception, options)
  selected_notifiers = options.delete(:notifiers) || notifiers
  [*selected_notifiers].each do |notifier|
    fire_notification(notifier, exception, options.dup)
  end
  true
end
register_exception_notifier(name, notifier_or_options) click to toggle source
# File lib/exception_notifier.rb, line 53
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
registered_exception_notifier(name) click to toggle source
# File lib/exception_notifier.rb, line 68
def registered_exception_notifier(name)
  @@notifiers[name]
end
testing_mode!() click to toggle source
# File lib/exception_notifier.rb, line 39
def testing_mode!
  self.testing_mode = true
end
unregister_exception_notifier(name) click to toggle source
# File lib/exception_notifier.rb, line 64
def unregister_exception_notifier(name)
  @@notifiers.delete(name)
end

Private Class Methods

create_and_register_notifier(name, options) click to toggle source
# File lib/exception_notifier.rb, line 113
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
fire_notification(notifier_name, exception, options) click to toggle source
# File lib/exception_notifier.rb, line 103
def fire_notification(notifier_name, exception, options)
  notifier = registered_exception_notifier(notifier_name)
  notifier.call(exception, options)
rescue Exception => e
  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
ignored?(exception, options) click to toggle source
# File lib/exception_notifier.rb, line 90
def ignored?(exception, options)
  @@ignores.any?{ |condition| condition.call(exception, options) }
rescue Exception => e
  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
ignored_exception?(ignore_array, exception) click to toggle source
# File lib/exception_notifier.rb, line 99
def ignored_exception?(ignore_array, exception)
  (Array(ignored_exceptions) + Array(ignore_array)).map(&:to_s).include?(exception.class.name)
end