Module: ExceptionNotifier

Includes:
ErrorGrouping
Defined in:
lib/exception_notifier.rb,
lib/exception_notifier/notifier.rb,
lib/exception_notifier/irc_notifier.rb,
lib/exception_notifier/sns_notifier.rb,
lib/exception_notifier/base_notifier.rb,
lib/exception_notifier/email_notifier.rb,
lib/exception_notifier/slack_notifier.rb,
lib/exception_notifier/teams_notifier.rb,
lib/exception_notifier/datadog_notifier.rb,
lib/exception_notifier/hipchat_notifier.rb,
lib/exception_notifier/webhook_notifier.rb,
lib/exception_notifier/modules/formatter.rb,
lib/exception_notifier/mattermost_notifier.rb,
lib/exception_notifier/google_chat_notifier.rb,
lib/exception_notifier/modules/error_grouping.rb,
lib/exception_notifier/modules/backtrace_cleaner.rb

Defined Under Namespace

Modules: BacktraceCleaner, ErrorGrouping Classes: BaseNotifier, DatadogNotifier, EmailNotifier, Formatter, GoogleChatNotifier, HipchatNotifier, IrcNotifier, MattermostNotifier, Notifier, SlackNotifier, SnsNotifier, TeamsNotifier, UndefinedNotifierError, WebhookNotifier

Constant Summary collapse

@@logger =
Logger.new(STDOUT)
@@ignored_exceptions =
%w[
  ActiveRecord::RecordNotFound Mongoid::Errors::DocumentNotFound AbstractController::ActionNotFound
  ActionController::RoutingError ActionController::UnknownFormat ActionController::UrlGenerationError
]
@@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



120
121
122
123
# File 'lib/exception_notifier.rb', line 120

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

.ignore_crawlers(crawlers) ⇒ Object



114
115
116
117
118
# File 'lib/exception_notifier.rb', line 114

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


106
107
108
# File 'lib/exception_notifier.rb', line 106

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

.ignore_notifier_if(notifier, &block) ⇒ Object



110
111
112
# File 'lib/exception_notifier.rb', line 110

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

.notifiersObject



97
98
99
# File 'lib/exception_notifier.rb', line 97

def notifiers
  @@notifiers.keys
end

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



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

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



78
79
80
81
82
83
84
85
86
# File 'lib/exception_notifier.rb', line 78

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



93
94
95
# File 'lib/exception_notifier.rb', line 93

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

.testing_mode!Object



53
54
55
# File 'lib/exception_notifier.rb', line 53

def testing_mode!
  self.testing_mode = true
end

.unregister_exception_notifier(name) ⇒ Object



89
90
91
# File 'lib/exception_notifier.rb', line 89

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