Module: ExceptionNotifier

Includes:
ErrorGrouping
Defined in:
lib/exception_notifier/notifier.rb,
lib/exception_notifier/db_notifier.rb,
lib/exception_notifier/base_notifier.rb,
lib/exception_notifier/modules/formatter.rb,
lib/exception_notifier/modules/error_grouping.rb,
lib/exception_notifier/modules/backtrace_cleaner.rb

Defined Under Namespace

Modules: BacktraceCleaner, ErrorGrouping Classes: BaseNotifier, DbNotifier, Formatter, UndefinedNotifierError

Constant Summary collapse

@@logger =
Logger.new($stdout)
@@ignored_exceptions =
%w[
  AbstractController::ActionNotFound
  ActionController::BadRequest
  ActionController::InvalidAuthenticityToken
  ActionController::InvalidCrossOriginRequest
  ActionController::ParameterMissing
  ActionController::RoutingError
  ActionController::UnknownFormat
  ActionController::UrlGenerationError
  ActionView::MissingTemplate
  ActionView::TemplateError
  ActiveRecord::RecordNotFound
  Mime::Type::InvalidMimeType
  Mongoid::Errors::DocumentNotFound
]
@@testing_mode =
false
@@ignores =

Store conditions that decide when exceptions must be ignored or not.

[]
@@by_notifier_ignores =

Store by-notifier conditions that decide when exceptions must be ignored or not.

{}
@@notifiers =

Store notifiers that send notifications when exceptions are raised.

{}

Class Method Summary collapse

Class Method Details

.clear_ignore_conditions!Object



122
123
124
125
# File 'lib/exception_notifier/notifier.rb', line 122

def clear_ignore_conditions!
  @@ignores.clear
  @@by_notifier_ignores.clear
end

.ignore_crawlers(crawlers) ⇒ Object



116
117
118
119
120
# File 'lib/exception_notifier/notifier.rb', line 116

def ignore_crawlers(crawlers)
  ignore_if do |_exception, opts|
    opts.key?(:env) && from_crawler(opts[:env], crawlers)
  end
end

.ignore_if(&block) ⇒ Object

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

ExceptionNotifier.ignore_if do |exception, options|
  not Rails.env.production?
end


108
109
110
# File 'lib/exception_notifier/notifier.rb', line 108

def ignore_if(&block)
  @@ignores << block
end

.ignore_notifier_if(notifier, &block) ⇒ Object



112
113
114
# File 'lib/exception_notifier/notifier.rb', line 112

def ignore_notifier_if(notifier, &block)
  @@by_notifier_ignores[notifier] = block
end

.notifiersObject



99
100
101
# File 'lib/exception_notifier/notifier.rb', line 99

def notifiers
  @@notifiers.keys
end

.notify_exception(exception, options = {}, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/exception_notifier/notifier.rb', line 59

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

.register_exception_notifier(name, notifier_or_options) ⇒ Object Also known as: add_notifier



80
81
82
83
84
85
86
87
88
# File 'lib/exception_notifier/notifier.rb', line 80

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

.registered_exception_notifier(name) ⇒ Object



95
96
97
# File 'lib/exception_notifier/notifier.rb', line 95

def registered_exception_notifier(name)
  @@notifiers[name]
end

.testing_mode!Object



55
56
57
# File 'lib/exception_notifier/notifier.rb', line 55

def testing_mode!
  self.testing_mode = true
end

.unregister_exception_notifier(name) ⇒ Object



91
92
93
# File 'lib/exception_notifier/notifier.rb', line 91

def unregister_exception_notifier(name)
  @@notifiers.delete(name)
end